-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyramidion.cpp
More file actions
650 lines (528 loc) · 17.4 KB
/
Copy pathpyramidion.cpp
File metadata and controls
650 lines (528 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Pass.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <sstream>
using namespace llvm;
//===----------------------------------------------------------------------===//
// Lexer
//===----------------------------------------------------------------------===//
enum Token {
tok_eof = -1,
tok_number = -2,
tok_operator = -3,
tok_identifier = -4,
tok_pyramid = -5,
tok_lparen = -6,
tok_rparen = -7,
tok_comma = -8,
tok_unknown = -9
};
static double NumVal; // Filled in if tok_number
static char OperatorChar; // Filled in if tok_operator
static std::string IdentifierStr; // Filled in if tok_identifier
/// Trim - Utility function to trim whitespace from a string.
static std::string Trim(const std::string &str) {
size_t first = str.find_first_not_of(" \t");
if (first == std::string::npos) return ""; // Empty or all spaces
size_t last = str.find_last_not_of(" \t");
return str.substr(first, (last - first + 1));
}
/// gettok - Return the next token from standard input.
static int gettok(const std::string &line, size_t &index) {
while (index < line.length() && isspace(line[index]))
++index;
if (index >= line.length())
return tok_eof;
if (isalpha(line[index])) { // Identifier: [a-zA-Z][a-zA-Z0-9_]*
IdentifierStr = "";
while (index < line.length() && (isalnum(line[index]) || line[index] == '_'))
IdentifierStr += line[index++];
if (IdentifierStr == "pyramid")
return tok_pyramid;
return tok_identifier;
}
if (isdigit(line[index]) || line[index] == '.') { // Number: [0-9.]+
std::string NumStr;
do {
NumStr += line[index];
++index;
} while (index < line.length() && (isdigit(line[index]) || line[index] == '.'));
// Validate the number format
if (NumStr.find("..") != std::string::npos) {
return tok_unknown; // Invalid number format
}
NumVal = strtod(NumStr.c_str(), nullptr);
return tok_number;
}
if (line[index] == '+' || line[index] == '-' || line[index] == '*' || line[index] == '/') { // Operator: [+-*/]
OperatorChar = line[index];
++index;
return tok_operator;
}
if (line[index] == '(') {
++index;
return tok_lparen;
}
if (line[index] == ')') {
++index;
return tok_rparen;
}
if (line[index] == ',') {
++index;
return tok_comma;
}
// Unknown character
++index;
return tok_unknown;
}
//===----------------------------------------------------------------------===//
// Abstract Syntax Tree
//===----------------------------------------------------------------------===//
namespace {
/// ExprAST - Base class for all expression nodes.
class ExprAST {
public:
virtual ~ExprAST() = default;
virtual void print(int depth = 0) const = 0;
virtual Value *codegen() = 0;
};
/// NumberExprAST - Expression class for numeric literals.
class NumberExprAST : public ExprAST {
double Val;
public:
NumberExprAST(double Val) : Val(Val) {}
void print(int depth = 0) const override {
for (int i = 0; i < depth; ++i)
std::cout << " ";
std::cout << "Number: " << Val << "\n";
}
Value *codegen() override;
};
/// VariableExprAST - Expression class for referencing a variable.
class VariableExprAST : public ExprAST {
std::string Name;
public:
VariableExprAST(const std::string &Name) : Name(Name) {}
const std::string &getName() const { return Name; }
void print(int depth = 0) const override {
for (int i = 0; i < depth; ++i)
std::cout << " ";
std::cout << "Variable: " << Name << "\n";
}
Value *codegen() override;
};
/// BinaryExprAST - Expression class for a binary operator.
class BinaryExprAST : public ExprAST {
char Op;
std::shared_ptr<ExprAST> LHS, RHS;
public:
BinaryExprAST(char Op) : Op(Op), LHS(nullptr), RHS(nullptr) {}
void setOperands(std::shared_ptr<ExprAST> L, std::shared_ptr<ExprAST> R) {
LHS = L;
RHS = R;
}
void print(int depth = 0) const override {
for (int i = 0; i < depth; ++i)
std::cout << " ";
std::cout << "Operator: " << Op << "\n";
if (LHS)
LHS->print(depth + 1);
if (RHS)
RHS->print(depth + 1);
}
Value *codegen() override;
};
/// CallExprAST - Expression class for function calls.
class FunctionCallExprAST : public ExprAST {
std::string Callee;
std::vector<std::shared_ptr<ExprAST>> Args;
public:
FunctionCallExprAST(const std::string &Callee) : Callee(Callee) {}
void setOperands(const std::vector<std::shared_ptr<ExprAST>> &Operands) {
Args = Operands;
}
const std::vector<std::shared_ptr<ExprAST>> &getArgs() const { return Args; }
void print(int depth = 0) const override {
for (int i = 0; i < depth; ++i)
std::cout << " ";
std::cout << "Function Call: " << Callee << "\n";
for (const auto &Arg : Args)
Arg->print(depth + 1);
}
Value *codegen() override;
};
/// PrototypeAST - This class represents the "prototype" for a function,
class PrototypeAST {
std::string Name;
std::vector<std::string> Args;
public:
PrototypeAST(const std::string &Name, std::vector<std::string> Args)
: Name(Name), Args(std::move(Args)) {}
Function *codegen();
const std::string &getName() const { return Name; }
const std::vector<std::string> &getArgs() const { return Args; }
};
/// FunctionAST - This class represents a function definition itself.
class FunctionAST {
std::unique_ptr<PrototypeAST> Proto;
std::shared_ptr<ExprAST> Body;
public:
FunctionAST(std::unique_ptr<PrototypeAST> Proto,
std::shared_ptr<ExprAST> Body)
: Proto(std::move(Proto)), Body(std::move(Body)) {}
Function *codegen();
};
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// Code Generation
//===----------------------------------------------------------------------===//
static std::unique_ptr<LLVMContext> TheContext;
static std::unique_ptr<Module> TheModule;
static std::unique_ptr<IRBuilder<>> Builder;
static std::map<std::string, Value *> NamedValues;
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
std::shared_ptr<ExprAST> LogError(const char *Str) {
fprintf(stderr, "Error: %s\n", Str);
return nullptr;
}
std::unique_ptr<PrototypeAST> LogErrorP(const char *Str) {
LogError(Str);
return nullptr;
}
Value *LogErrorV(const char *Str) {
LogError(Str);
return nullptr;
}
Value *NumberExprAST::codegen() {
return ConstantFP::get(*TheContext, APFloat(Val));
}
Value *VariableExprAST::codegen() {
Value *V = NamedValues[Name];
if (!V)
return LogErrorV(("Unknown variable name: " + Name).c_str());
return V;
}
Value *BinaryExprAST::codegen() {
Value *L = LHS->codegen();
Value *R = RHS->codegen();
if (!L || !R)
return nullptr;
switch (Op) {
case '+':
return Builder->CreateFAdd(L, R, "addtmp");
case '-':
return Builder->CreateFSub(L, R, "subtmp");
case '*':
return Builder->CreateFMul(L, R, "multmp");
case '/':
return Builder->CreateFDiv(L, R, "divtmp");
default:
return LogErrorV("invalid binary operator");
}
}
Value *FunctionCallExprAST::codegen() {
Function *CalleeF = TheModule->getFunction(Callee);
if (!CalleeF)
return LogErrorV(("Unknown function referenced: " + Callee).c_str());
if (CalleeF->arg_size() != Args.size())
return LogErrorV("Incorrect number of arguments passed");
std::vector<Value *> ArgsV;
for (auto &Arg : Args) {
Value *ArgV = Arg->codegen();
if (!ArgV)
return nullptr;
ArgsV.push_back(ArgV);
}
return Builder->CreateCall(CalleeF, ArgsV, "calltmp");
}
Function *PrototypeAST::codegen() {
std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(*TheContext));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(*TheContext), Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
unsigned Idx = 0;
for (auto &Arg : F->args())
Arg.setName(Args[Idx++]);
return F;
}
Function *FunctionAST::codegen() {
Function *TheFunction = TheModule->getFunction(Proto->getName());
if (!TheFunction)
TheFunction = Proto->codegen();
if (!TheFunction)
return nullptr;
if (!TheFunction->empty()) {
LogErrorV("Function cannot be redefined.");
return nullptr;
}
BasicBlock *BB = BasicBlock::Create(*TheContext, "entry", TheFunction);
Builder->SetInsertPoint(BB);
NamedValues.clear();
for (auto &Arg : TheFunction->args())
NamedValues[std::string(Arg.getName())] = &Arg;
if (Value *RetVal = Body->codegen()) {
Builder->CreateRet(RetVal);
verifyFunction(*TheFunction, &errs());
TheFPM->run(*TheFunction);
return TheFunction;
}
TheFunction->eraseFromParent();
return nullptr;
}
//===----------------------------------------------------------------------===//
// Parser
//===----------------------------------------------------------------------===//
std::shared_ptr<ExprAST> BuildAST(const std::vector<std::vector<std::shared_ptr<ExprAST>>> &ASTLevels, size_t &levelIndex, size_t &nodeIndex) {
if (levelIndex >= ASTLevels.size() || nodeIndex >= ASTLevels[levelIndex].size())
return nullptr;
auto node = ASTLevels[levelIndex][nodeIndex++];
if (auto *BinOp = dynamic_cast<BinaryExprAST*>(node.get())) {
size_t nextLevel = levelIndex + 1;
if (nextLevel >= ASTLevels.size()) {
LogError("Error: Incomplete pyramid structure for binary operator.");
return nullptr;
}
size_t leftNodeIndex = nodeIndex - 1;
size_t rightNodeIndex = nodeIndex;
auto LHS = BuildAST(ASTLevels, nextLevel, leftNodeIndex);
auto RHS = BuildAST(ASTLevels, nextLevel, rightNodeIndex);
if (!LHS || !RHS) {
LogError("Error: Missing operands for binary operator.");
return nullptr;
}
BinOp->setOperands(LHS, RHS);
return node;
} else if (auto *VarNode = dynamic_cast<VariableExprAST*>(node.get())) {
// Variable or function call
if (NamedValues.find(VarNode->getName()) == NamedValues.end()) {
// Not a known variable, treat it as a function call
Function *CalleeF = TheModule->getFunction(VarNode->getName());
if (!CalleeF) {
LogError(("Unknown function referenced: " + VarNode->getName()).c_str());
return nullptr;
}
size_t nextLevel = levelIndex + 1;
if (nextLevel >= ASTLevels.size()) {
LogError("Error: Incomplete pyramid structure for function call.");
return nullptr;
}
size_t childNodeIndex = 0;
std::vector<std::shared_ptr<ExprAST>> Args;
for (unsigned i = 0; i < CalleeF->arg_size(); ++i) {
auto Arg = BuildAST(ASTLevels, nextLevel, childNodeIndex);
if (!Arg) {
LogError("Error: Missing arguments for function call.");
return nullptr;
}
Args.push_back(Arg);
}
auto FuncCall = std::make_shared<FunctionCallExprAST>(VarNode->getName());
FuncCall->setOperands(Args);
return FuncCall;
} else {
// Known variable
return node;
}
} else if (auto *NumNode = dynamic_cast<NumberExprAST*>(node.get())) {
return node;
}
return nullptr;
}
static bool ParsePyramid(std::istream &input) {
std::string line;
std::vector<std::string> lines;
std::vector<std::vector<std::shared_ptr<ExprAST>>> ASTLevels;
// Read lines until we encounter an empty line or EOF
while (std::getline(input, line)) {
line = Trim(line);
if (line.empty()) {
break; // End of the current pyramid
}
lines.push_back(line); // Store the line
std::vector<std::shared_ptr<ExprAST>> ASTNodes;
size_t index = 0;
while (index < line.size()) {
int tok = gettok(line, index);
if (tok == tok_eof)
break;
if (tok == tok_number) {
ASTNodes.push_back(std::make_shared<NumberExprAST>(NumVal));
} else if (tok == tok_operator) {
ASTNodes.push_back(std::make_shared<BinaryExprAST>(OperatorChar));
} else if (tok == tok_identifier) {
ASTNodes.push_back(std::make_shared<VariableExprAST>(IdentifierStr));
} else if (tok == tok_unknown) {
LogError("Error: Unknown token encountered.");
return false;
}
}
ASTLevels.push_back(std::move(ASTNodes));
}
if (ASTLevels.empty()) {
return false;
}
bool isFunctionDefinition = false;
std::string funcName;
std::vector<std::string> funcArgs;
// Get the last line
std::string lastLine = lines.back();
size_t index = 0;
int tok = gettok(lastLine, index);
if (tok == tok_pyramid) {
isFunctionDefinition = true;
// Rest of the line after 'pyramid'
std::string rest = lastLine.substr(index);
rest = Trim(rest);
size_t pos = rest.find('(');
if (pos != std::string::npos) {
funcName = rest.substr(0, pos);
funcName = Trim(funcName);
std::string argsStr = rest.substr(pos + 1);
// Remove closing ')'
if (!argsStr.empty() && argsStr.back() == ')')
argsStr.pop_back();
// Split arguments by ','
std::istringstream argsStream(argsStr);
std::string arg;
while (std::getline(argsStream, arg, ',')) {
// Trim whitespace
arg = Trim(arg);
funcArgs.push_back(arg);
}
} else {
LogError("Error: Invalid function definition.");
return true;
}
// Remove the last level as it's the function definition
ASTLevels.pop_back();
lines.pop_back();
}
// Set up known variables
if (isFunctionDefinition) {
NamedValues.clear();
for (const auto &arg : funcArgs) {
NamedValues[arg] = nullptr;
}
} else {
NamedValues.clear();
}
// Build the AST recursively
std::shared_ptr<ExprAST> Root;
size_t levelIndex = 0;
size_t nodeIndex = 0;
Root = BuildAST(ASTLevels, levelIndex, nodeIndex);
if (!Root) {
LogError("Error: Failed to build AST.");
return true;
}
if (isFunctionDefinition) {
auto Proto = std::make_unique<PrototypeAST>(funcName, funcArgs);
auto FnAST = std::make_unique<FunctionAST>(std::move(Proto), Root);
if (auto *FnIR = FnAST->codegen()) {
FnIR->print(errs());
}
} else {
// Generate a 'main' function to evaluate the expression
auto Proto = std::make_unique<PrototypeAST>("main", std::vector<std::string>());
auto FnAST = std::make_unique<FunctionAST>(std::move(Proto), Root);
if (auto *FnIR = FnAST->codegen()) {
FnIR->print(errs());
}
}
return true;
}
//===----------------------------------------------------------------------===//
// Top-Level parsing
//===----------------------------------------------------------------------===//
void InitializeModule() {
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("Pyramidion JIT", *TheContext);
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Create the pass manager
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Add some optimizations
TheFPM->add(createInstructionCombiningPass());
TheFPM->add(createReassociatePass());
TheFPM->add(createGVNPass());
TheFPM->add(createCFGSimplificationPass());
TheFPM->doInitialization();
}
//===----------------------------------------------------------------------===//
// Main driver code.
//===----------------------------------------------------------------------===//
int main(int argc, char **argv) {
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <input file>\n";
return 1;
}
std::ifstream inputFile(argv[1]);
if (!inputFile) {
std::cerr << "Error: Cannot open file " << argv[1] << "\n";
return 1;
}
InitializeModule();
// Parse pyramids
while (true) {
bool parsed = ParsePyramid(inputFile);
if (!parsed)
break;
}
// Verify the module
if (verifyModule(*TheModule, &errs())) {
errs() << "Error: module verification failed\n";
return 1;
}
// Set up the ExecutionEngine
std::string ErrStr;
ExecutionEngine *EE = EngineBuilder(std::move(TheModule))
.setErrorStr(&ErrStr)
.setEngineKind(EngineKind::JIT)
.create();
if (!EE) {
errs() << "Failed to construct ExecutionEngine: " << ErrStr << "\n";
return 1;
}
// Run the 'main' function if it exists
Function *MainFunction = EE->FindFunctionNamed("main");
if (MainFunction) {
std::vector<GenericValue> Args; // Add arguments if needed
GenericValue Result = EE->runFunction(MainFunction, Args);
outs() << "Result: " << Result.DoubleVal << "\n";
} else {
errs() << "No 'main' function found in module.\n";
}
return 0;
}