Skip to content

Commit

Permalink
Fix Class CodeGen
Browse files Browse the repository at this point in the history
  • Loading branch information
fly-lang committed Jun 12, 2024
1 parent af90f4b commit 9f13eda
Show file tree
Hide file tree
Showing 25 changed files with 805 additions and 855 deletions.
5 changes: 4 additions & 1 deletion docs/c++_llvm/oo/constructor1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ class Test {

int main() {
Test *t = new Test(1);
return t->getA();
int a = t->getA();
a = t->getA();
delete t;
return a;
}
68 changes: 42 additions & 26 deletions docs/c++_llvm/oo/constructor1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,50 @@ define dso_local i32 @main() #0 personality i8* bitcast (i32 (...)* @__gxx_perso
%2 = alloca %class.Test*, align 8
%3 = alloca i8*, align 8
%4 = alloca i32, align 4
%5 = alloca i32, align 4
store i32 0, i32* %1, align 4
%5 = call noalias nonnull i8* @_Znwm(i64 4) #4
%6 = bitcast i8* %5 to %class.Test*
invoke void @_ZN4TestC2Ei(%class.Test* %6, i32 1)
to label %7 unwind label %10

7: ; preds = %0
store %class.Test* %6, %class.Test** %2, align 8
%8 = load %class.Test*, %class.Test** %2, align 8
%9 = call i32 @_ZN4Test4getAEv(%class.Test* %8)
ret i32 %9

10: ; preds = %0
%11 = landingpad { i8*, i32 }
%6 = call noalias nonnull i8* @_Znwm(i64 4) #4
%7 = bitcast i8* %6 to %class.Test*
invoke void @_ZN4TestC2Ei(%class.Test* %7, i32 1)
to label %8 unwind label %19

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 = icmp eq %class.Test* %13, null
br i1 %14, label %17, label %15

15: ; preds = %8
%16 = bitcast %class.Test* %13 to i8*
call void @_ZdlPv(i8* %16) #5
br label %17

17: ; preds = %15, %8
%18 = load i32, i32* %5, align 4
ret i32 %18

19: ; preds = %0
%20 = landingpad { i8*, i32 }
cleanup
%12 = extractvalue { i8*, i32 } %11, 0
store i8* %12, i8** %3, align 8
%13 = extractvalue { i8*, i32 } %11, 1
store i32 %13, i32* %4, align 4
call void @_ZdlPv(i8* %5) #5
br label %14

14: ; preds = %10
%15 = load i8*, i8** %3, align 8
%16 = load i32, i32* %4, align 4
%17 = insertvalue { i8*, i32 } undef, i8* %15, 0
%18 = insertvalue { i8*, i32 } %17, i32 %16, 1
resume { i8*, i32 } %18
%21 = extractvalue { i8*, i32 } %20, 0
store i8* %21, i8** %3, align 8
%22 = extractvalue { i8*, i32 } %20, 1
store i32 %22, i32* %4, align 4
call void @_ZdlPv(i8* %6) #5
br label %23

23: ; preds = %19
%24 = load i8*, i8** %3, align 8
%25 = load i32, i32* %4, align 4
%26 = insertvalue { i8*, i32 } undef, i8* %24, 0
%27 = insertvalue { i8*, i32 } %26, i32 %25, 1
resume { i8*, i32 } %27
}

; Function Attrs: nobuiltin allocsize(0)
Expand Down
12 changes: 4 additions & 8 deletions docs/c++_llvm/oo/struct.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
struct Test {
int a = 1;
int a;
int b;
};

Test getTest() {
Test t;
return t;
}

int main() {
Test *t = new Test();
int a = t->a;
t->a = 1;
t->b = 2;
return getTest().a;
delete t;
return 1;
}
73 changes: 40 additions & 33 deletions docs/c++_llvm/oo/struct.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,50 @@ target triple = "x86_64-pc-linux-gnu"

%struct.Test = type { i32, i32 }

$_ZN4TestC2Ev = comdat any

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i64 @_Z7getTestv() #0 {
%1 = alloca %struct.Test, align 4
call void @_ZN4TestC2Ev(%struct.Test* %1) #2
%2 = bitcast %struct.Test* %1 to i64*
%3 = load i64, i64* %2, align 4
ret i64 %3
}

; Function Attrs: noinline nounwind optnone uwtable
define linkonce_odr dso_local void @_ZN4TestC2Ev(%struct.Test* %0) unnamed_addr #0 comdat align 2 {
%2 = alloca %struct.Test*, align 8
store %struct.Test* %0, %struct.Test** %2, align 8
%3 = load %struct.Test*, %struct.Test** %2, align 8
%4 = getelementptr inbounds %struct.Test, %struct.Test* %3, i32 0, i32 0
store i32 1, i32* %4, align 4
ret void
}

; Function Attrs: noinline norecurse nounwind optnone uwtable
define dso_local i32 @main() #1 {
; Function Attrs: noinline norecurse optnone uwtable
define dso_local i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca %struct.Test, align 4
%2 = alloca %struct.Test*, align 8
store i32 0, i32* %1, align 4
%3 = call i64 @_Z7getTestv()
%4 = bitcast %struct.Test* %2 to i64*
store i64 %3, i64* %4, align 4
%5 = getelementptr inbounds %struct.Test, %struct.Test* %2, i32 0, i32 0
%6 = load i32, i32* %5, align 4
ret i32 %6
%3 = call noalias nonnull i8* @_Znwm(i64 8) #4
%4 = bitcast i8* %3 to %struct.Test*
%5 = bitcast %struct.Test* %4 to i8*
call void @llvm.memset.p0i8.i64(i8* align 8 %5, i8 0, i64 8, i1 false)
store %struct.Test* %4, %struct.Test** %2, align 8
%6 = load %struct.Test*, %struct.Test** %2, align 8
%7 = getelementptr inbounds %struct.Test, %struct.Test* %6, i32 0, i32 0
store i32 1, i32* %7, align 4
%8 = load %struct.Test*, %struct.Test** %2, align 8
%9 = getelementptr inbounds %struct.Test, %struct.Test* %8, i32 0, i32 1
store i32 2, i32* %9, align 4
%10 = load %struct.Test*, %struct.Test** %2, align 8
%11 = icmp eq %struct.Test* %10, null
br i1 %11, label %14, label %12

12: ; preds = %0
%13 = bitcast %struct.Test* %10 to i8*
call void @_ZdlPv(i8* %13) #5
br label %14

14: ; preds = %12, %0
ret i32 1
}

attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nounwind }
; Function Attrs: nobuiltin allocsize(0)
declare dso_local nonnull i8* @_Znwm(i64) #1

; Function Attrs: argmemonly nounwind willreturn writeonly
declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nobuiltin nounwind
declare dso_local void @_ZdlPv(i8*) #3

attributes #0 = { noinline norecurse optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nobuiltin allocsize(0) "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { argmemonly nounwind willreturn writeonly }
attributes #3 = { nobuiltin nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #4 = { builtin allocsize(0) }
attributes #5 = { builtin nounwind }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
Expand Down
2 changes: 1 addition & 1 deletion include/AST/ASTCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace fly {
class ASTParam;
class ASTArg;
class ASTCallExpr;
class ASTVar;
class ASTExpr;
class ASTVar;

enum class ASTCallKind {
CALL_FUNCTION,
Expand Down
2 changes: 1 addition & 1 deletion include/AST/ASTFunctionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace fly {

llvm::SmallVector<ASTLocalVar *, 8> getLocalVars() const;

const ASTBlockStmt *getBody() const;
ASTBlockStmt *getBody() const;

void setErrorHandler(ASTParam *ErrorHandler);

Expand Down
2 changes: 1 addition & 1 deletion include/CodeGen/CodeGenClassFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace fly {

friend class CodeGenClass;

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

public:

Expand Down
6 changes: 6 additions & 0 deletions include/CodeGen/CodeGenError.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ namespace fly {

llvm::Value *Pointer = nullptr;

llvm::LoadInst *LoadI = nullptr;

llvm::StringRef BlockID;

public:

CodeGenError(CodeGenModule *CGM, ASTVar *Error);
Expand All @@ -40,6 +44,8 @@ namespace fly {

llvm::Type *getType();

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

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

llvm::LoadInst *Load() override;
Expand Down
50 changes: 0 additions & 50 deletions include/CodeGen/CodeGenInstance.h

This file was deleted.

19 changes: 8 additions & 11 deletions include/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ namespace fly {
friend class CodeGenClassVar;
friend class CodeGenClassFunction;
friend class CodeGenVarBase;
friend class CodeGenInstance;
friend class CodeGenVar;
friend class CodeGenExpr;
friend class CodeGenError;
Expand Down Expand Up @@ -189,7 +188,11 @@ namespace fly {

llvm::Constant *GenValue(const ASTType *Type, const ASTValue *Val);

void GenStmt(CodeGenFunctionBase *CGF, ASTStmt * Stmt);
// llvm::Value *Convert(llvm::Value *V, llvm::Type *T);

llvm::Value *ConvertToBool(llvm::Value *Val);

llvm::Value *Convert(llvm::Value *FromVal, const ASTType *FromType, const ASTType *ToType);

CodeGenError *GenErrorHandler(ASTVar* Var);

Expand All @@ -201,7 +204,9 @@ namespace fly {

llvm::Value *GenExpr(ASTExpr *Expr);

void GenBlock(CodeGenFunctionBase *CGF, const llvm::SmallVector<ASTStmt *, 8> &Content, llvm::BasicBlock *BB = nullptr);
void GenStmt(CodeGenFunctionBase *CGF, ASTStmt * Stmt);

void GenBlock(CodeGenFunctionBase *CGF, ASTBlockStmt *BlockStmt);

void GenIfBlock(CodeGenFunctionBase *CGF, ASTIfStmt *If);

Expand All @@ -213,15 +218,7 @@ namespace fly {

void GenLoopBlock(CodeGenFunctionBase *CGF, ASTLoopStmt *Loop);

void pushArgs(ASTCall *pCall, llvm::SmallVector<llvm::Value *, 8> &Args);

void GenReturn(ASTFunctionBase *CGF, ASTExpr *Expr = nullptr);

llvm::Value *ConvertToBool(llvm::Value *Val);

// llvm::Value *Convert(llvm::Value *V, llvm::Type *T);

llvm::Value *Convert(llvm::Value *FromVal, const ASTType *FromType, const ASTType *ToType);
};
}

Expand Down
2 changes: 0 additions & 2 deletions include/CodeGen/CodeGenVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ namespace fly {

llvm::LoadInst *LoadI = nullptr;

llvm::StoreInst *StoreI = nullptr;

llvm::StringRef BlockID;

public:
Expand Down
2 changes: 1 addition & 1 deletion include/Lex/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ namespace fly {
/// produces comment and whitespace tokens.
///
/// This requires the lexer to have an associated preprocessor. A standalone
/// lexer has nothing to Init to.
/// lexer has nothing to Instantiate to.
void resetExtendedTokenMode();

/// Gets source code buffer.
Expand Down
4 changes: 2 additions & 2 deletions include/Lex/MultipleIncludeOpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IdentifierInfo;
class MultipleIncludeOpt {
/// ReadAnyTokens - This is set to false when a file is first opened and true
/// any time a token is returned to the client or a (non-multiple-include)
/// directive is parsed. When the final \#endif is parsed this is Init back
/// directive is parsed. When the final \#endif is parsed this is Instantiate back
/// to false, that way any tokens before the first \#ifdef or after the last
/// \#endif can be easily detected.
bool ReadAnyTokens;
Expand All @@ -41,7 +41,7 @@ class MultipleIncludeOpt {

/// ReadAnyTokens - This is set to false when a file is first opened and true
/// any time a token is returned to the client or a (non-multiple-include)
/// directive is parsed. When the final #endif is parsed this is Init back
/// directive is parsed. When the final #endif is parsed this is Instantiate back
/// to false, that way any tokens before the first #ifdef or after the last
/// #endif can be easily detected.
bool DidMacroExpansion;
Expand Down
6 changes: 3 additions & 3 deletions include/Sema/SemaBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ namespace fly {
// Create Call
ASTCall *CreateCall(ASTIdentifier *Identifier);

ASTCall *CreateCall(ASTFunction *Function);
ASTCall *CreateCall(ASTFunction *Function, ASTVar *ErrorHandler);

ASTCall *CreateCall(ASTClassMethod *Method);
ASTCall *CreateCall(ASTClassMethod *Method, ASTVar *ErrorHandler);

ASTCall *CreateCall(ASTIdentifier *Instance, ASTClassMethod *Method);
ASTCall *CreateCall(ASTIdentifier *Instance, ASTClassMethod *Method, ASTVar *ErrorHandler);

ASTArg *CreateArg(ASTExpr *Expr);

Expand Down
2 changes: 1 addition & 1 deletion src/AST/ASTFunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ llvm::SmallVector<ASTLocalVar *, 8> ASTFunctionBase::getLocalVars() const {
return LocalVars;
}

const ASTBlockStmt *ASTFunctionBase::getBody() const {
ASTBlockStmt *ASTFunctionBase::getBody() const {
return Body;
}

Expand Down
Loading

0 comments on commit 9f13eda

Please sign in to comment.