alloc, load, store, and ret are all in the IR
This commit is contained in:
@@ -38,7 +38,15 @@ char* gravityFunctionToCStr(GravityFunction function);
|
||||
char* gravityValueToCStr(GravityValue value);
|
||||
|
||||
void gravityPositionBuilderAtEnd(GravityBuilder builder, GravityBlock block);
|
||||
|
||||
GravityValue gravityNewIntConstant(GravityType intType, int64_t value);
|
||||
|
||||
// instructions
|
||||
GravityValue gravityBuildAlloc(GravityBuilder builder, GravityType type, const char* name);
|
||||
void gravityBuildStore(GravityBuilder builder, GravityValue pointer, GravityValue value);
|
||||
GravityValue gravityBuildLoad(GravityBuilder builder, GravityValue pointer, const char* name);
|
||||
void gravityBuildRet(GravityBuilder builder, GravityValue returnValue);
|
||||
|
||||
// ============================ //
|
||||
|
||||
#endif
|
||||
@@ -13,11 +13,11 @@ char* gravityBlockToCStr(GravityBlock block) {
|
||||
sbAppend(&buff, "%s", gravityTypeToCStr(block->paramTypes[i]));
|
||||
}
|
||||
}
|
||||
sbAppend(&buff, "):\n");
|
||||
sbAppend(&buff, "):");
|
||||
|
||||
for (unsigned int i = 0; i < block->instructions.count; i++) {
|
||||
GravityInstruction inst = *get(block->instructions, i);
|
||||
sbAppend(&buff, " %s\n", gravityInstructionToCStr(inst));
|
||||
sbAppend(&buff, "\n %s", gravityInstructionToCStr(inst));
|
||||
}
|
||||
|
||||
return buff.data;
|
||||
|
||||
@@ -4,10 +4,16 @@ void gravityBuildInstruction(GravityBuilder builder, GravityOpcode opcode, Gravi
|
||||
assert(builder->currentBlock != NULL);
|
||||
|
||||
// copy operands list to avoid stack lifetime
|
||||
GravityValue* operandsCopy = calloc(numOperands, sizeof(GravityValue));
|
||||
|
||||
GravityValue* operandsCopy = NULL;
|
||||
|
||||
if (operands != NULL) {
|
||||
operandsCopy = calloc(numOperands, sizeof(GravityValue));
|
||||
if (!operandsCopy)
|
||||
return;
|
||||
memcpy(operandsCopy, operands, sizeof(GravityValue) * numOperands);
|
||||
}
|
||||
|
||||
|
||||
GravityInstruction newInst = {
|
||||
.opcode = opcode,
|
||||
@@ -42,3 +48,47 @@ GravityValue gravityBuildAlloc(GravityBuilder builder, GravityType type, const c
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void gravityBuildStore(GravityBuilder builder, GravityValue pointer, GravityValue value) {
|
||||
gravityBuildInstruction(
|
||||
builder,
|
||||
GravityOpcodeStore,
|
||||
(GravityValue[]){pointer, value},
|
||||
2,
|
||||
NULL
|
||||
);
|
||||
}
|
||||
|
||||
GravityValue gravityBuildLoad(GravityBuilder builder, GravityValue pointer, const char* name) {
|
||||
GravityValue result = gravityNewValue(name, gravityCreatePointerType());
|
||||
|
||||
gravityBuildInstruction(
|
||||
builder,
|
||||
GravityOpcodeLoad,
|
||||
(GravityValue[]){pointer},
|
||||
1,
|
||||
result
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void gravityBuildRet(GravityBuilder builder, GravityValue returnValue) {
|
||||
if (returnValue != NULL) {
|
||||
gravityBuildInstruction(
|
||||
builder,
|
||||
GravityOpcodeRet,
|
||||
(GravityValue[]){returnValue},
|
||||
1,
|
||||
NULL
|
||||
);
|
||||
} else {
|
||||
gravityBuildInstruction(
|
||||
builder,
|
||||
GravityOpcodeRet,
|
||||
NULL,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -16,5 +16,8 @@ GravityBuilder gravityCreateBuilder(void);
|
||||
void gravityPositionBuilderAtEnd(GravityBuilder builder, GravityBlock block);
|
||||
|
||||
GravityValue gravityBuildAlloc(GravityBuilder builder, GravityType type, const char* name);
|
||||
void gravityBuildStore(GravityBuilder builder, GravityValue pointer, GravityValue value);
|
||||
GravityValue gravityBuildLoad(GravityBuilder builder, GravityValue pointer, const char* name);
|
||||
void gravityBuildRet(GravityBuilder builder, GravityValue returnValue);
|
||||
|
||||
#endif
|
||||
@@ -5,6 +5,7 @@ char* gravityOpcodeToCStr(GravityOpcode opcode) {
|
||||
case GravityOpcodeAlloc: return "alloc";
|
||||
case GravityOpcodeStore: return "store";
|
||||
case GravityOpcodeLoad: return "load";
|
||||
case GravityOpcodeRet: return "ret";
|
||||
default: return "fixme";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
typedef enum {
|
||||
GravityOpcodeAlloc = 0,
|
||||
GravityOpcodeStore,
|
||||
GravityOpcodeLoad
|
||||
GravityOpcodeLoad,
|
||||
GravityOpcodeRet
|
||||
} GravityOpcode;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -84,7 +84,7 @@ GravityValue gravityNewConstant(GravityType type) {
|
||||
return newValue;
|
||||
}
|
||||
|
||||
GravityValue newIntConstant(GravityType intType, int64_t value) {
|
||||
GravityValue gravityNewIntConstant(GravityType intType, int64_t value) {
|
||||
assert(intType->typeKind == GravityIntTypeKind);
|
||||
|
||||
GravityValue newValue = gravityNewConstant(intType);
|
||||
|
||||
@@ -35,6 +35,6 @@ GravityValue gravityTypeToTypeValue(GravityType type);
|
||||
char* gravityValueToCStr(GravityValue value);
|
||||
|
||||
GravityValue gravityNewConstant(GravityType type);
|
||||
GravityValue newIntConstant(GravityType intType, int64_t value);
|
||||
GravityValue gravityNewIntConstant(GravityType intType, int64_t value);
|
||||
|
||||
#endif
|
||||
8
test.c
8
test.c
@@ -12,7 +12,13 @@ int main(void) {
|
||||
GravityBlock entryBlock = gravityStartBlock("entry", mainFunc, 0);
|
||||
|
||||
gravityPositionBuilderAtEnd(builder, entryBlock);
|
||||
GravityValue something = gravityBuildAlloc(builder, charType, NULL);
|
||||
GravityValue someVar = gravityBuildAlloc(builder, charType, NULL);
|
||||
|
||||
GravityValue someConstant = gravityNewIntConstant(charType, 123);
|
||||
gravityBuildStore(builder, someVar, someConstant);
|
||||
|
||||
GravityValue output = gravityBuildLoad(builder, someVar, NULL);
|
||||
gravityBuildRet(builder, output);
|
||||
|
||||
printf("%s\n", gravityFunctionToCStr(mainFunc));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user