From 19abc8496f5cb171024e4f0fb2fc88135e009cc3 Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Wed, 5 Aug 2026 20:55:35 +1000 Subject: [PATCH] calculate index of each instruction in function and calculate block preds and succs --- Makefile | 2 +- include/gravity/core.h | 1 + src/ir/block.c | 3 +++ src/ir/block.h | 4 ++++ src/ir/function.c | 26 +++++++++++++++++++++++++ src/ir/function.h | 4 ++-- src/ir/instruction.h | 1 + src/ir/module.c | 1 + test.c | 44 +++++++----------------------------------- 9 files changed, 46 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 8b89b60..ec5a543 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ SRC=src OBJ=obj CC=gcc -CFLAGS=-Wall -Wextra -fPIC -O3# -fsanitize=address -g +CFLAGS=-Wall -Wextra -fPIC -O3# -g -fsanitize=address TARGET=libgravity.so TEST_TARGET=testapp diff --git a/include/gravity/core.h b/include/gravity/core.h index bbb5dc6..e3367e0 100644 --- a/include/gravity/core.h +++ b/include/gravity/core.h @@ -27,6 +27,7 @@ GravityBuilder gravityCreateBuilder(void); GravityFunction gravityNewFunction(GravityModule module, const char* name, GravityType functionType); GravityBlock gravityStartBlock(const char* name, GravityFunction function); +void gravityCompleteFunction(GravityFunction function); int gravityGetTypeSize(GravityType type); diff --git a/src/ir/block.c b/src/ir/block.c index 94f5d23..359c1d4 100644 --- a/src/ir/block.c +++ b/src/ir/block.c @@ -9,6 +9,9 @@ GravityBlock gravityNewBlock(const char* name) { newBlock->params = newList(GravityBlockParam); newBlock->instructions = newList(GravityInstruction); + newBlock->predecessors = newList(GravityBlock); + newBlock->successors = newList(GravityBlock); + return newBlock; } diff --git a/src/ir/block.h b/src/ir/block.h index 88cebfc..5d03498 100644 --- a/src/ir/block.h +++ b/src/ir/block.h @@ -14,12 +14,16 @@ typedef struct { UseList(GravityBlockParam); UseList(GravityInstruction); +UseList(GravityBlock); struct GravityOpaqueBlock { const char* name; List(GravityBlockParam) params; List(GravityValue) values; List(GravityInstruction) instructions; + + List(GravityBlock) predecessors; + List(GravityBlock) successors; }; char* gravityBlockToCStr(GravityBlock block); diff --git a/src/ir/function.c b/src/ir/function.c index 5e1d78a..2c2bdef 100644 --- a/src/ir/function.c +++ b/src/ir/function.c @@ -4,11 +4,37 @@ GravityBlock gravityStartBlock(const char* name, GravityFunction function) { GravityBlock newBlock = gravityNewBlock(name); if (newBlock == NULL) return newBlock; + size_t newBlockIndex = function->blocks.count; + + for (size_t blockIdx = 0; blockIdx < newBlockIndex; blockIdx++) { + GravityBlock predecessor = *get(function->blocks, blockIdx); + + append(predecessor->successors, newBlock); + append(newBlock->predecessors, predecessor); + } + append(function->blocks, newBlock); return newBlock; } +void gravityCompleteFunction(GravityFunction function) { + assert(!function->completed); + function->completed = true; + + uint64_t instructionIndex = 0; + for (size_t blockIdx = 0; blockIdx < function->blocks.count; blockIdx++) { + GravityBlock block = *get(function->blocks, blockIdx); + + for (size_t instIdx = 0; instIdx < block->instructions.count; instIdx++) { + GravityInstruction* inst = get(block->instructions, instIdx); + + inst->indexInFunction = instructionIndex; + instructionIndex++; + } + } +} + char* gravityFunctionToCStr(GravityFunction function) { StringBuffer buff; initStringBuff(&buff); diff --git a/src/ir/function.h b/src/ir/function.h index 91007f6..a149265 100644 --- a/src/ir/function.h +++ b/src/ir/function.h @@ -10,17 +10,17 @@ #include #include -UseList(GravityBlock); - typedef struct GravityOpaqueFunction* GravityFunction; struct GravityOpaqueFunction { GravityFunctionType functionType; List(GravityBlock) blocks; const char* name; + bool completed; }; GravityBlock gravityStartBlock(const char* name, GravityFunction function); char* gravityFunctionToCStr(GravityFunction function); +void gravityCompleteFunction(GravityFunction function); #endif \ No newline at end of file diff --git a/src/ir/instruction.h b/src/ir/instruction.h index b94c6d0..608489f 100644 --- a/src/ir/instruction.h +++ b/src/ir/instruction.h @@ -21,6 +21,7 @@ struct GravityInstruction { GravityValue* operands; size_t numOperands; GravityValue result; + uint64_t indexInFunction; }; char* gravityInstructionToCStr(GravityInstruction inst); diff --git a/src/ir/module.c b/src/ir/module.c index 2296d69..80a6353 100644 --- a/src/ir/module.c +++ b/src/ir/module.c @@ -18,6 +18,7 @@ GravityFunction gravityNewFunction(GravityModule module, const char* name, Gravi newFunc->name = strdup(name); newFunc->blocks = newList(GravityBlock); newFunc->functionType = functionType->function; + newFunc->completed = false; append(module->functions, newFunc); diff --git a/test.c b/test.c index ed86450..2b397bb 100644 --- a/test.c +++ b/test.c @@ -11,54 +11,24 @@ int main(void) { GravityType mainFuncType = gravityCreateFunctionType(0, NULL, charType); GravityFunction mainFunc = gravityNewFunction(module, "main", mainFuncType); GravityBlock entryBlock = gravityStartBlock("entry", mainFunc); - - /* - // define blocks GravityBlock thenBlock = gravityStartBlock("then", mainFunc); - gravityAppendBlockArgument(thenBlock, ptrType); - GravityBlock elseBlock = gravityStartBlock("else", mainFunc); - gravityAppendBlockArgument(elseBlock, ptrType); - GravityBlock mergeBlock = gravityStartBlock("merge", mainFunc); - gravityAppendBlockArgument(mergeBlock, ptrType); - - // write entry gravityBuilderSwitchToBlock(builder, entryBlock); - GravityValue x = gravityBuildAlloc(builder, charType, "x"); - GravityValue oneConst = gravityBuildIntConstant(builder, charType, 1, "true"); - gravityBuildBr(builder, oneConst, thenBlock, (GravityValue[]){x}, elseBlock, (GravityValue[]){x}); + GravityValue oneConst = gravityNewIntConstant(charType, 1); + gravityBuildBr(builder, oneConst, thenBlock, (GravityValue[]){}, elseBlock, (GravityValue[]){}); - // then block gravityBuilderSwitchToBlock(builder, thenBlock); - GravityValue thenValue = gravityGetBlockArg(thenBlock, 0); - GravityValue fiveConst = gravityBuildIntConstant(builder, charType, 5, "five"); - gravityBuildStore(builder, thenValue, fiveConst); - gravityBuildJump(builder, mergeBlock, (GravityValue[]){thenValue}); + gravityBuildJump(builder, mergeBlock, (GravityValue[]){}); - // else block gravityBuilderSwitchToBlock(builder, elseBlock); - GravityValue elseValue = gravityGetBlockArg(elseBlock, 0); - GravityValue tenConst = gravityBuildIntConstant(builder, charType, 10, "ten"); - gravityBuildStore(builder, elseValue, tenConst); - gravityBuildJump(builder, mergeBlock, (GravityValue[]){elseValue}); - - // merge block + gravityBuildJump(builder, mergeBlock, (GravityValue[]){}); + gravityBuilderSwitchToBlock(builder, mergeBlock); - GravityValue xVar = gravityGetBlockArg(mergeBlock, 0); - GravityValue xValue = gravityBuildLoad(builder, xVar, "xValue"); - gravityBuildRet(builder, xValue);*/ + gravityBuildRet(builder, NULL); - gravityBuilderSwitchToBlock(builder, entryBlock); - - GravityValue variable = gravityBuildAlloc(builder, charType, NULL); - GravityValue returnVal = gravityNewIntConstant(charType, 123);//gravityBuildIntConstant(builder, charType, 123, NULL); - gravityBuildStore(builder, variable, returnVal); - - - GravityValue varValue = gravityBuildLoad(builder, variable, NULL); - gravityBuildRet(builder, varValue); + gravityCompleteFunction(mainFunc); printf("%s\n", gravityFunctionToCStr(mainFunc));