Skip to content

Commit

Permalink
Fix the bugs of array
Browse files Browse the repository at this point in the history
  • Loading branch information
XChy committed May 23, 2023
1 parent dff8655 commit b53cb94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
10 changes: 10 additions & 0 deletions LLVMIR/ClassProxy/MemberExprProxy.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "MemberExprProxy.h"
#include <llvm-14/llvm/IR/Value.h>
#include <llvm/ADT/APInt.h>
#include "LLVMIR/CodeGenHelper.h"
#include "LLVMIR/CodeGenProxy.h"
Expand Down Expand Up @@ -32,6 +33,15 @@ ValueAndType CodeGenProxy<MemberExprNode>::codeGen(MemberExprNode *ast,
}
index++;
}
} else if (obj_type->isArray()) {
if (ast->memberName() == "length") {
llvm::Value *length_ptr = helper->llvm_builder.CreateStructGEP(
obj->getType()->getContainedType(0), obj, 0);

llvm::Value *length = helper->llvm_builder.CreateLoad(
length_ptr->getType()->getContainedType(0), length_ptr);
return {deReference({length, XSharp::getI32Type()}, helper)};
}
}

helper->error("No member '{}' for '{}'", ast->memberName(),
Expand Down
2 changes: 1 addition & 1 deletion LLVMIR/FuncProxies/FuncDefinitionProxies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ ValueAndType CodeGenProxy<FunctionNode>::codeGen(FunctionNode* ast,
// llvm::verifyFunction(*func);

// optimize the function
helper->optimizer.functionPassManager.run(*func);
// helper->optimizer.functionPassManager.run(*func);

return {func, functionType};
}
2 changes: 1 addition & 1 deletion LLVMIR/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ llvm::Value* genArrayMalloc(CodeGenContext* helper, XSharp::Type* type,
auto elements_ptr_ptr = helper->llvm_builder.CreateStructGEP(
array_struct_type, array_value, 1);

helper->llvm_builder.CreateStore(sizeofElement, length_ptr);
helper->llvm_builder.CreateStore(element_count, length_ptr);
helper->llvm_builder.CreateStore(elements_ptr, elements_ptr_ptr);

return array_value;
Expand Down
28 changes: 26 additions & 2 deletions testcases/string.xsharp
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
class string {
char[] data;

new(char[] chars) {
self.data = new char[](chars.length);
i64 i = 0;
while(i < chars.length) {
self.data[i] = chars[i];
i = i + 1;
}
}

char charAt(i64 index) {
return self.data[index];
}

char[] getData(){
print(self.data.length);
return self.data;
}
}

i32 main(){
char[] a = "123123";
print(a);
string a = new string("H,world!\n");
print("H,world!\n");
print(a.getData());
print(a.charAt(3));
return 0;
}

0 comments on commit b53cb94

Please sign in to comment.