Expression execution seems to be working
This commit is contained in:
2
make.sh
2
make.sh
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
cc -g -std=c++17 -lc++ src/*.cpp -o brb
|
cc -g -std=c++17 -lc++ -lllvm -DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1 -L/usr/local/opt/llvm/lib -I/usr/local/opt/llvm/include src/*.cpp -o brb
|
||||||
42
src/Compiler.cpp
Normal file
42
src/Compiler.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include "Compiler.h"
|
||||||
|
#include "llvm/IR/LLVMContext.h"
|
||||||
|
#include "llvm/IR/Constants.h"
|
||||||
|
#include "llvm/Support/raw_ostream.h"
|
||||||
|
|
||||||
|
Compiler::Compiler(shared_ptr<Expression> expression): expression(expression) {
|
||||||
|
context = make_shared<llvm::LLVMContext>();
|
||||||
|
module = make_shared<llvm::Module>("dummy", *context);
|
||||||
|
builder = make_shared<llvm::IRBuilder<>>(*context);
|
||||||
|
|
||||||
|
//int32Type = shared_ptr<llvm::IntegerType>(llvm::Type::getInt32Ty(context));
|
||||||
|
int32Type = llvm::Type::getInt32Ty(*context);
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::Value *Compiler::valueForExpression(shared_ptr<Expression> expression) {
|
||||||
|
switch (expression->getKind()) {
|
||||||
|
case Expression::Kind::LITERAL:
|
||||||
|
return llvm::ConstantInt::get(int32Type, expression->getInteger(), true);
|
||||||
|
case Expression::Kind::BINARY:
|
||||||
|
llvm::Value *leftValue = valueForExpression(expression->getLeft());
|
||||||
|
llvm::Value *rightValue = valueForExpression(expression->getRight());
|
||||||
|
switch (expression->getOperator()) {
|
||||||
|
case Expression::Operator::ADD:
|
||||||
|
return builder->CreateNSWAdd(leftValue, rightValue);
|
||||||
|
case Expression::Operator::SUB:
|
||||||
|
return builder->CreateNSWSub(leftValue, rightValue);
|
||||||
|
case Expression::Operator::MUL:
|
||||||
|
return builder->CreateNSWMul(leftValue, rightValue);
|
||||||
|
case Expression::Operator::DIV:
|
||||||
|
return builder->CreateSDiv(leftValue, rightValue);
|
||||||
|
case Expression::Operator::MOD:
|
||||||
|
return builder->CreateSRem(leftValue, rightValue);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<llvm::Module> Compiler::getModule() {
|
||||||
|
llvm::Value *value = valueForExpression(expression);
|
||||||
|
value->print(llvm::outs(), false);
|
||||||
|
return module;
|
||||||
|
}
|
||||||
28
src/Compiler.h
Normal file
28
src/Compiler.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef COMPILER_H
|
||||||
|
#define COMPILER_H
|
||||||
|
|
||||||
|
#include "llvm/IR/Module.h"
|
||||||
|
#include "llvm/IR/IRBuilder.h"
|
||||||
|
#include "Expression.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Compiler {
|
||||||
|
private:
|
||||||
|
shared_ptr<llvm::LLVMContext> context;
|
||||||
|
//llvm::LLVMContext context;
|
||||||
|
shared_ptr<llvm::Module> module;
|
||||||
|
shared_ptr<llvm::IRBuilder<>> builder;
|
||||||
|
|
||||||
|
//shared_ptr<llvm::IntegerType> int32Type;
|
||||||
|
llvm::IntegerType *int32Type;
|
||||||
|
|
||||||
|
shared_ptr<Expression> expression;
|
||||||
|
llvm::Value *valueForExpression(shared_ptr<Expression> expression);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Compiler(shared_ptr<Expression> expression);
|
||||||
|
shared_ptr<llvm::Module> getModule();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -62,6 +62,22 @@ Expression::Kind Expression::getKind() {
|
|||||||
return kind;
|
return kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t Expression::getInteger() {
|
||||||
|
return integer;
|
||||||
|
}
|
||||||
|
|
||||||
|
Expression::Operator Expression::getOperator() {
|
||||||
|
return operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<Expression> Expression::getLeft() {
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<Expression> Expression::getRight() {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
|
||||||
bool Expression::operator==(Expression const& other) {
|
bool Expression::operator==(Expression const& other) {
|
||||||
return kind == other.kind;
|
return kind == other.kind;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public:
|
|||||||
INVALID
|
INVALID
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Operation {
|
enum Operator {
|
||||||
ADD,
|
ADD,
|
||||||
SUB,
|
SUB,
|
||||||
MUL,
|
MUL,
|
||||||
@@ -25,7 +25,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
Kind kind = INVALID;
|
Kind kind = INVALID;
|
||||||
int64_t integer = 0;
|
int64_t integer = 0;
|
||||||
Operation operation = NONE;
|
Operator operation = NONE;
|
||||||
shared_ptr<Expression> left = nullptr;
|
shared_ptr<Expression> left = nullptr;
|
||||||
shared_ptr<Expression> right = nullptr;
|
shared_ptr<Expression> right = nullptr;
|
||||||
|
|
||||||
@@ -35,6 +35,10 @@ private:
|
|||||||
public:
|
public:
|
||||||
Expression(Kind kind, Token token, shared_ptr<Expression> left, shared_ptr<Expression> right);
|
Expression(Kind kind, Token token, shared_ptr<Expression> left, shared_ptr<Expression> right);
|
||||||
Kind getKind();
|
Kind getKind();
|
||||||
|
int64_t getInteger();
|
||||||
|
Operator getOperator();
|
||||||
|
shared_ptr<Expression> getLeft();
|
||||||
|
shared_ptr<Expression> getRight();
|
||||||
bool operator==(Expression const& other);
|
bool operator==(Expression const& other);
|
||||||
bool operator!=(Expression const& other);
|
bool operator!=(Expression const& other);
|
||||||
string toString();
|
string toString();
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
|
|
||||||
#include "Expression.h"
|
#include "Expression.h"
|
||||||
#include "Parser.h"
|
#include "Parser.h"
|
||||||
|
#include "Compiler.h"
|
||||||
|
|
||||||
|
#include "llvm/Support/raw_ostream.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -40,5 +43,10 @@ int main(int argc, char **argv) {
|
|||||||
shared_ptr<Expression> expression = parser.getExpression();
|
shared_ptr<Expression> expression = parser.getExpression();
|
||||||
cout << expression->toString() << endl;
|
cout << expression->toString() << endl;
|
||||||
|
|
||||||
|
Compiler compiler(expression);
|
||||||
|
compiler.getModule();
|
||||||
|
shared_ptr<llvm::Module> module = compiler.getModule();
|
||||||
|
module->print(llvm::outs(), nullptr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user