add some memory sys calls

pull/11/head
cosmonaut 2021-06-04 00:41:30 -07:00
parent 12ac9cc980
commit 01da2dc377
2 changed files with 78 additions and 10 deletions

View File

@ -21,7 +21,12 @@ struct MemoryBlock<T>
Get(index: uint): T Get(index: uint): T
{ {
return @bitcast<T>(AddressOf(index)); return @dereference<T>(AddressOf(index));
}
Set(index: uint, value: T): void
{
@memcpy(AddressOf(index), @addr(value), @sizeof<T>());
} }
Free(): void Free(): void
@ -34,13 +39,19 @@ struct Program {
static Main(): int { static Main(): int {
x: int = 4; x: int = 4;
y: int = Foo.Func(x); y: int = Foo.Func(x);
block: MemoryBlock<uint>; block: MemoryBlock<int>;
block.capacity = y; block.capacity = y;
block.start = @malloc(y * @sizeof<int>()); block.start = @malloc(y * @sizeof<int>());
z: MemoryAddress = block.AddressOf(2); z: MemoryAddress = block.AddressOf(2);
Console.PrintLine("%u", block.Get(0));
Console.PrintLine("%p", block.start); 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(); block.Free();
return 0; return 0;
} }

View File

@ -1474,6 +1474,39 @@ static LLVMValueRef CompileSystemCallExpression(
return LLVMSizeOf(ResolveType(ConcretizeType(typeTag))); 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 ( else if (
strcmp( strcmp(
systemCallExpression->systemCall.identifier->identifier.name, systemCallExpression->systemCall.identifier->identifier.name,
@ -1484,12 +1517,7 @@ static LLVMValueRef CompileSystemCallExpression(
->genericArguments.arguments[0] ->genericArguments.arguments[0]
->type.typeNode->typeTag); ->type.typeNode->typeTag);
LLVMValueRef expression = CompileExpression( LLVMValueRef expression = args[0];
structTypeDeclaration,
selfParam,
builder,
systemCallExpression->systemCall.argumentSequence
->functionArgumentSequence.sequence[0]);
return LLVMBuildBitCast( return LLVMBuildBitCast(
builder, builder,
@ -2301,6 +2329,35 @@ static void RegisterLibraryFunctions(
AddSystemFunction("free", freeFunctionType, freeFunction); 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); LLVMDisposeBuilder(builder);
} }