Skip to content

Commit

Permalink
Fix Class CodeGen Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
fly-lang committed Jun 29, 2024
1 parent 9fa9c56 commit c2014fc
Show file tree
Hide file tree
Showing 27 changed files with 288 additions and 208 deletions.
21 changes: 21 additions & 0 deletions docs/c++_llvm/new.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <malloc.h>
#include "stdio.h"

struct Test {
int a;
int b;
};

int main() {
// Allocate memory for one instance of struct Test
struct Test *ptr = (struct Test *)malloc(sizeof(struct Test));

// Initialize the struct members
ptr->a = 10;
ptr->b = 20;

// Free the allocated memory
free(ptr);

return 0;
}
3 changes: 2 additions & 1 deletion docs/c++_llvm/oo/constructor1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ int main() {
Test *t = new Test(1);
int a = t->getA();
a = t->getA();
float b = t->b;
t->b = 44;
t->b = 22;
delete t;
return a;
}
43 changes: 22 additions & 21 deletions docs/c++_llvm/oo/constructor1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@ define dso_local i32 @main() #0 personality i8* bitcast (i32 (...)* @__gxx_perso
%3 = alloca i8*, align 8
%4 = alloca i32, align 4
%5 = alloca i32, align 4
%6 = alloca float, align 4
store i32 0, i32* %1, align 4
%7 = call noalias nonnull i8* @_Znwm(i64 8) #4
%8 = bitcast i8* %7 to %class.Test*
invoke void @_ZN4TestC2Ei(%class.Test* %8, i32 1)
to label %9 unwind label %23

9: ; preds = %0
store %class.Test* %8, %class.Test** %2, align 8
%10 = load %class.Test*, %class.Test** %2, align 8
%11 = call i32 @_ZN4Test4getAEv(%class.Test* %10)
store i32 %11, i32* %5, align 4
%12 = load %class.Test*, %class.Test** %2, align 8
%13 = call i32 @_ZN4Test4getAEv(%class.Test* %12)
store i32 %13, i32* %5, align 4
%14 = load %class.Test*, %class.Test** %2, align 8
%15 = getelementptr inbounds %class.Test, %class.Test* %14, i32 0, i32 1
%16 = load float, float* %15, align 4
store float %16, float* %6, align 4
%6 = call noalias nonnull i8* @_Znwm(i64 8) #4
%7 = bitcast i8* %6 to %class.Test*
invoke void @_ZN4TestC2Ei(%class.Test* %7, i32 1)
to label %8 unwind label %23

8: ; preds = %0
store %class.Test* %7, %class.Test** %2, align 8
%9 = load %class.Test*, %class.Test** %2, align 8
%10 = call i32 @_ZN4Test4getAEv(%class.Test* %9)
store i32 %10, i32* %5, align 4
%11 = load %class.Test*, %class.Test** %2, align 8
%12 = call i32 @_ZN4Test4getAEv(%class.Test* %11)
store i32 %12, i32* %5, align 4
%13 = load %class.Test*, %class.Test** %2, align 8
%14 = getelementptr inbounds %class.Test, %class.Test* %13, i32 0, i32 1
store float 4.400000e+01, float* %14, align 4
%15 = load %class.Test*, %class.Test** %2, align 8
%16 = getelementptr inbounds %class.Test, %class.Test* %15, i32 0, i32 1
store float 2.200000e+01, float* %16, align 4
%17 = load %class.Test*, %class.Test** %2, align 8
%18 = icmp eq %class.Test* %17, null
br i1 %18, label %21, label %19

19: ; preds = %9
19: ; preds = %8
%20 = bitcast %class.Test* %17 to i8*
call void @_ZdlPv(i8* %20) #5
br label %21

21: ; preds = %19, %9
21: ; preds = %19, %8
%22 = load i32, i32* %5, align 4
ret i32 %22

Expand All @@ -55,7 +56,7 @@ define dso_local i32 @main() #0 personality i8* bitcast (i32 (...)* @__gxx_perso
store i8* %25, i8** %3, align 8
%26 = extractvalue { i8*, i32 } %24, 1
store i32 %26, i32* %4, align 4
call void @_ZdlPv(i8* %7) #5
call void @_ZdlPv(i8* %6) #5
br label %27

27: ; preds = %23
Expand Down
9 changes: 9 additions & 0 deletions docs/c++_llvm/reference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
void ref(int &a) {
a = 2;
}

int main() {
int a = 1;
ref(a);
return 0;
}
2 changes: 0 additions & 2 deletions include/AST/ASTFunctionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ namespace fly {

ASTBlockStmt *getBody() const;

void setErrorHandler(ASTParam *ErrorHandler);

ASTParam *getErrorHandler();

virtual CodeGenFunctionBase *getCodeGen() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/CodeGen/CodeGenClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace fly {

llvm::StructType *getVTableType();

const llvm::SmallVector<CodeGenClassVar *, 4> &getAttributes() const;
// const llvm::SmallVector<CodeGenClassVar *, 4> &getAttributes() const;

const llvm::SmallVector<CodeGenClassFunction *, 4> &getConstructors() const;

Expand Down
3 changes: 3 additions & 0 deletions include/CodeGen/CodeGenClassFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ namespace fly {

class CodeGenModule;
class CodeGenClass;
class CodeGenVar;
class ASTClassMethod;

class CodeGenClassFunction : public CodeGenFunctionBase {

friend class CodeGenClass;

SmallVector<CodeGenVar *, 4> Attributes;

CodeGenClassFunction(CodeGenModule *CGM, ASTClassMethod *AST, llvm::PointerType *TypePtr);

public:
Expand Down
10 changes: 3 additions & 7 deletions include/CodeGen/CodeGenClassVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace fly {

llvm::Value *Pointer = nullptr;

llvm::Value *Instance = nullptr;

llvm::StringRef BlockID;

llvm::Type *ClassType = nullptr;
Expand All @@ -48,12 +50,10 @@ namespace fly {

llvm::LoadInst *LoadI = nullptr;

llvm::Value *Instance = nullptr;

public:
CodeGenClassVar(CodeGenModule *CGM, ASTClassAttribute *Var, llvm::Type *ClassType, uint32_t Index);

void Init() override;
void setInstance(llvm::Value *Inst);

llvm::StoreInst *Store(llvm::Value *Val) override;

Expand All @@ -63,11 +63,7 @@ namespace fly {

llvm::Value *getPointer() override;

ASTVar *getVar() override;

llvm::Value *getIndex();

void setInstance(llvm::Value *Inst);
};
}

Expand Down
10 changes: 6 additions & 4 deletions include/CodeGen/CodeGenEnumEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ namespace fly {

llvm::Value *Value;

llvm::Value *Instance = nullptr;

public:
CodeGenEnumEntry(CodeGenModule *CGM, ASTEnumEntry *EnumEntry);

llvm::Value *getValue() override;

void Init() override;
// llvm::AllocaInst *Alloca() override;

llvm::StoreInst *Store(llvm::Value *Val) override;

llvm::LoadInst *Load() override;

llvm::Value *getValue() override;

llvm::Value *getPointer() override;

ASTVar *getVar() override;
CodeGenVarBase *getVar(llvm::StringRef Name);
};
}

Expand Down
12 changes: 7 additions & 5 deletions include/CodeGen/CodeGenError.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ namespace fly {

llvm::Value *Pointer = nullptr;

llvm::Value *Instance = nullptr;

llvm::LoadInst *LoadI = nullptr;

llvm::StringRef BlockID;

public:

CodeGenError(CodeGenModule *CGM, ASTVar *Error);
CodeGenError(CodeGenModule *CGM, ASTVar *Error, llvm::Value *Pointer);

static llvm::StructType *GenErrorType(llvm::LLVMContext &LLVMCtx);

void Init() override;

llvm::Type *getType();

llvm::StoreInst *StorePointer(llvm::Value *Val);

// llvm::AllocaInst *Alloca() override;

llvm::StoreInst *Store(llvm::Value *Val) override;

llvm::LoadInst *Load() override;
Expand All @@ -54,9 +56,9 @@ namespace fly {

llvm::Value *getPointer() override;

ASTVar *getVar() override;

void Store(ASTExpr *Expr);

CodeGenVarBase *getVar(llvm::StringRef Name);
};
}

Expand Down
6 changes: 4 additions & 2 deletions include/CodeGen/CodeGenGlobalVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ namespace fly {

llvm::Value *Pointer = nullptr;

llvm::Value *Instance = nullptr;

llvm::LoadInst *LoadI = nullptr;

public:
CodeGenGlobalVar(CodeGenModule *CGM, ASTGlobalVar* Var, bool isExternal = false);

void Init();
// llvm::AllocaInst *Alloca() override;

llvm::StoreInst *Store(llvm::Value *Val) override;

Expand All @@ -48,7 +50,7 @@ namespace fly {

llvm::Value *getPointer() override;

ASTVar *getVar() override;
CodeGenVarBase *getVar(llvm::StringRef Name);
};
}

Expand Down
4 changes: 2 additions & 2 deletions include/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace fly {
class CodeGenFunction;
class CodeGenFunctionBase;
class CodeGenClass;
class CodeGenVarBase;
class CodeGenVar;
class CodeGenEnum;
class CodeGenError;

Expand Down Expand Up @@ -196,7 +196,7 @@ namespace fly {

CodeGenError *GenErrorHandler(ASTVar* Var);

CodeGenVarBase *GenVar(ASTVar* Var);
CodeGenVar *GenLocalVar(ASTLocalVar* Var);

llvm::Value *GenVarRef(ASTVarRef *VarRef);

Expand Down
27 changes: 21 additions & 6 deletions include/CodeGen/CodeGenVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
#define FLY_CG_VAR_H

#include "CodeGenVarBase.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringMap.h"

namespace llvm {
class Type;
class StringRef;
}

namespace fly {
Expand All @@ -29,20 +30,34 @@ namespace fly {

CodeGenModule *CGM = nullptr;

ASTVar *Var = nullptr;
CodeGenVar *Parent = nullptr;

llvm::StringMap<CodeGenVar *> Vars;

llvm::Type *T = nullptr;

llvm::Value *Pointer = nullptr;
llvm::AllocaInst *Pointer = nullptr;

llvm::LoadInst *LoadI = nullptr;

llvm::StringRef BlockID;

uint32_t Index = 0;

public:
CodeGenVar(CodeGenModule *CGM, ASTVar *Var);
// CodeGenVar(CodeGenModule *CGM, ASTVar *Var);

CodeGenVar(CodeGenModule *CGM, llvm::Type *T);

CodeGenVar(CodeGenModule *CGM, llvm::Type *Ty, CodeGenVar *Parent, uint32_t Index);

CodeGenVar *getParent();

CodeGenVarBase *getVar(llvm::StringRef Name);

llvm::Type *getType();

void Init() override;
llvm::AllocaInst *Alloca();

llvm::StoreInst *Store(llvm::Value *Val) override;

Expand All @@ -52,7 +67,7 @@ namespace fly {

llvm::Value *getPointer() override;

ASTVar *getVar() override;
void addVar(llvm::StringRef Name, CodeGenVar *CGV);
};
}

Expand Down
6 changes: 3 additions & 3 deletions include/CodeGen/CodeGenVarBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace llvm {
class StoreInst;
class LoadInst;
class AllocaInst;
class Value;
class StringRef;
}

namespace fly {
Expand All @@ -25,8 +27,6 @@ namespace fly {

public:

virtual void Init() = 0;

virtual llvm::StoreInst *Store(llvm::Value *Val) = 0;

virtual llvm::LoadInst *Load() = 0;
Expand All @@ -35,7 +35,7 @@ namespace fly {

virtual llvm::Value *getPointer() = 0;

virtual ASTVar *getVar() = 0;
virtual CodeGenVarBase *getVar(llvm::StringRef Name) = 0;
};
}

Expand Down
4 changes: 0 additions & 4 deletions src/AST/ASTFunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ ASTBlockStmt *ASTFunctionBase::getBody() const {
return Body;
}

void ASTFunctionBase::setErrorHandler(ASTParam *EH) {
ErrorHandler = EH;
}

ASTParam *ASTFunctionBase::getErrorHandler() {
return ErrorHandler;
}
Expand Down
Loading

0 comments on commit c2014fc

Please sign in to comment.