calculate index of each instruction in function and calculate block preds and succs
This commit is contained in:
2
Makefile
2
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
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -10,17 +10,17 @@
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
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
|
||||
@@ -21,6 +21,7 @@ struct GravityInstruction {
|
||||
GravityValue* operands;
|
||||
size_t numOperands;
|
||||
GravityValue result;
|
||||
uint64_t indexInFunction;
|
||||
};
|
||||
|
||||
char* gravityInstructionToCStr(GravityInstruction inst);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
42
test.c
42
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});
|
||||
gravityBuildJump(builder, mergeBlock, (GravityValue[]){});
|
||||
|
||||
// merge block
|
||||
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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user