From 01da2dc3774085caf30260e34bc0aeb5a990628e Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 4 Jun 2021 00:41:30 -0700 Subject: [PATCH] add some memory sys calls --- generic.w | 19 +++++++++++--- src/codegen.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/generic.w b/generic.w index c127e17..ba8d511 100644 --- a/generic.w +++ b/generic.w @@ -21,7 +21,12 @@ struct MemoryBlock Get(index: uint): T { - return @bitcast(AddressOf(index)); + return @dereference(AddressOf(index)); + } + + Set(index: uint, value: T): void + { + @memcpy(AddressOf(index), @addr(value), @sizeof()); } Free(): void @@ -34,13 +39,19 @@ struct Program { static Main(): int { x: int = 4; y: int = Foo.Func(x); - block: MemoryBlock; + block: MemoryBlock; block.capacity = y; block.start = @malloc(y * @sizeof()); z: MemoryAddress = block.AddressOf(2); - Console.PrintLine("%u", block.Get(0)); Console.PrintLine("%p", block.start); - Console.PrintLine("%p", z); + block.Set(0, 5); + block.Set(1, 3); + block.Set(2, 9); + block.Set(3, 100); + Console.PrintLine("%i", block.Get(0)); + Console.PrintLine("%i", block.Get(1)); + Console.PrintLine("%i", block.Get(2)); + Console.PrintLine("%i", block.Get(3)); block.Free(); return 0; } diff --git a/src/codegen.c b/src/codegen.c index 991098f..f9cbf7b 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1474,6 +1474,39 @@ static LLVMValueRef CompileSystemCallExpression( return LLVMSizeOf(ResolveType(ConcretizeType(typeTag))); } + else if ( + strcmp( + systemCallExpression->systemCall.identifier->identifier.name, + "addr") == 0) + { + return LLVMBuildPtrToInt( + builder, + FindVariablePointer( + systemCallExpression->systemCall.argumentSequence + ->functionArgumentSequence.sequence[0] + ->identifier.name), + LLVMInt64Type(), + "addrResult"); + } + else if ( + strcmp( + systemCallExpression->systemCall.identifier->identifier.name, + "dereference") == 0) + { + TypeTag *typeTag = ConcretizeType( + systemCallExpression->systemCall.genericArguments + ->genericArguments.arguments[0] + ->type.typeNode->typeTag); + + return LLVMBuildLoad( + builder, + LLVMBuildIntToPtr( + builder, + args[0], + LLVMPointerType(ResolveType(typeTag), 0), + "deref_ptr"), + "deref"); + } else if ( strcmp( systemCallExpression->systemCall.identifier->identifier.name, @@ -1484,12 +1517,7 @@ static LLVMValueRef CompileSystemCallExpression( ->genericArguments.arguments[0] ->type.typeNode->typeTag); - LLVMValueRef expression = CompileExpression( - structTypeDeclaration, - selfParam, - builder, - systemCallExpression->systemCall.argumentSequence - ->functionArgumentSequence.sequence[0]); + LLVMValueRef expression = args[0]; return LLVMBuildBitCast( builder, @@ -2301,6 +2329,35 @@ static void RegisterLibraryFunctions( AddSystemFunction("free", freeFunctionType, freeFunction); + LLVMTypeRef memcpyParams[3]; + memcpyParams[0] = LLVMInt64Type(); + memcpyParams[1] = LLVMInt64Type(); + memcpyParams[2] = LLVMInt64Type(); + LLVMTypeRef memcpyFunctionType = + LLVMFunctionType(LLVMVoidType(), memcpyParams, 3, 0); + LLVMValueRef memcpyFunction = + LLVMAddFunction(module, "memcopy", memcpyFunctionType); + + LLVMBasicBlockRef memcpyEntry = + LLVMAppendBasicBlock(memcpyFunction, "entry"); + LLVMPositionBuilderAtEnd(builder, memcpyEntry); + LLVMValueRef dest = LLVMBuildIntToPtr( + builder, + LLVMGetParam(memcpyFunction, 0), + LLVMPointerType(LLVMInt64Type(), 0), + "dest"); + LLVMValueRef src = LLVMBuildIntToPtr( + builder, + LLVMGetParam(memcpyFunction, 1), + LLVMPointerType(LLVMInt64Type(), 0), + "src"); + + LLVMBuildMemCpy(builder, dest, 8, src, 8, LLVMGetParam(memcpyFunction, 2)); + + LLVMBuildRetVoid(builder); + + AddSystemFunction("memcpy", memcpyFunctionType, memcpyFunction); + LLVMDisposeBuilder(builder); }