From 408c50b34489d2c59b05a2815b95b758794848bd Mon Sep 17 00:00:00 2001 From: LucasMW Date: Sun, 26 Sep 2021 21:37:00 +0100 Subject: [PATCH 01/20] basic --- Makefile | 2 +- src/codellvm.c | 1133 ++++++++++++++++++++++++++++++++++++++++++++++++ src/codellvm.h | 7 + src/main.c | 17 +- 4 files changed, 1154 insertions(+), 5 deletions(-) create mode 100644 src/codellvm.c create mode 100644 src/codellvm.h diff --git a/Makefile b/Makefile index ec122b6..1682240 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS = -Wall -std=c99 -O3 OUTFILE = hac -SOURCES = src/main.c src/lex.c src/grammar.c src/tree.c src/lextest.c src/symbolTable.c src/codeGen.c src/testbfi.c src/compilerFunctions.c src/codeEss.c src/optimizer.c src/expander.c src/highlight.c +SOURCES = src/main.c src/lex.c src/grammar.c src/tree.c src/lextest.c src/symbolTable.c src/codeGen.c src/testbfi.c src/compilerFunctions.c src/codeEss.c src/optimizer.c src/expander.c src/highlight.c src/codellvm.c OBJS = temp/codeGen.o temp/symbolTable.o temp/grammar.o temp/tree.o temp/main.o temp/lex.o temp/lextest.o temp/testbfi.o temp/compilerFunctions.o temp/codeEss.o temp/optimizer.o temp/expander.o temp/highlight.o ZIGCC = zig cc diff --git a/src/codellvm.c b/src/codellvm.c new file mode 100644 index 0000000..ef403bc --- /dev/null +++ b/src/codellvm.c @@ -0,0 +1,1133 @@ +#include +#include +#include +#include +#include +#if !defined(tree_h) + #include "tree.h" + #define tree_h +#endif + +static void codeDefVar(DefVar* dv); +static void codeDefFunc(DefFunc* df); +static void codeDefVarList(DefVarL* dvl); +static void codeDefList(Def* d); +static void codeNameList(NameL* nl,Type* t,int scope); +static void codeType(Type* t); +static void codeParams(Parameter* params); +static void codeForAllocParams(Parameter* params); +static void codeCommandList(CommandL* cl); +static int codeExpAccess(Exp* e); +static void codeBlock(Block* b); +static int codeExp(Exp* e); +static void codeVar(Var* v); +static void codeConstant(Constant* c); +static void codeExpList(ExpList* el); +static int codeAccessElemPtr(Exp* e); +static char* stringForType(Type* t); + +static FILE* output = NULL; +static int currentFunctionTIndex = 0; +static int currentBrIndex = 0; + +static int currentStringConstant = 0; +static int declareTop = 0; +char* stringsToDeclare[100]; + +static int currentFuncHasReturn = 0; + + +void setCodeOutputLLVM(FILE* out) { + output = out; +} + +static void defaultOutput() { + output = stdout; +} + + +static Parameter* currentParameters = NULL; + +char* stringForType(Type* t) { + char* tStr = NULL; + if(t == NULL) + return "void"; + switch(t->tag) { + case base: + switch(t->b) { + case WInt: + return "i32"; + break; + case WFloat: + return "float"; + break; + case WChar: + return "i8"; + break; + case WByte: + return "i32"; + case WShort: + return "i16"; + } + break; + case array: + + tStr = stringForType(t->of); + int tlen = strlen(tStr); + char* new = malloc(tlen+2); + strcpy(new,tStr); + //free(tStr); // no need anymore + new[tlen] = '*'; + new[tlen+1] = '\0'; + return new; + + break; + } +} + +static char* stringForDefaultValue(Type* t) { + if(!t) { + printf(";That's probably an error\n"); + return "void"; + } + if(t->tag == base) { + if(t->b == WInt || t->b == WChar || t->b == WByte) { + return "0"; + } + else { + return "0.0"; + } + } else { + return "null"; + } + +} +static void codeDefaultReturn(Type* t) { + if(!t) { + fprintf(output, "ret void\n"); + return; + } + char* tStr = stringForType(t); + char* value = stringForDefaultValue(t); + fprintf(output, "ret %s %s\n", tStr,value); +} + +static void pushStringToDeclare(char* str) { + //printf("%s\n",str ); + char* nstr = malloc(strlen(str)+1); + strcpy(nstr,str); + nstr[strlen(str)] = '\0'; + stringsToDeclare[declareTop] = nstr ; + declareTop++; +} +static void declateStringsToDeclare() { + int x = currentStringConstant - declareTop; + for(int i = 0;i= 50) { + printf("SevereError. var name is to big\n"); + } + if(scope == 0) { + sprintf(string,"@g%s",name); + } + else { + sprintf(string,"%%l%d%s",scope,name); + } + + char* str = (char*)malloc(strlen(string)+1); + strcpy(str,string); + str[strlen(string)] = '\0'; + return str; +} +static void codeExtraDeclares() { + //fprintf(output, "declare noalias i8* @malloc(i64)\n" ); + fprintf(output, "; generated with mongaComp \n" ); + fprintf(output, "declare i8* @malloc(i32)\n" ); //malloc + fprintf(output, "declare i32 @printf(i8* nocapture readonly, ...)\n" ); + fprintf(output, "declare i32 @puts(i8* nocapture readonly)\n" ); + fprintf(output, "@.intprintstr = private unnamed_addr constant [3 x i8] c\"%%d\\00\"\n" ); + fprintf(output, "@.floatprintstr = private unnamed_addr constant [3 x i8] c\"%%f\\00\"\n" ); + fprintf(output, "@.charprintstr = private unnamed_addr constant [3 x i8] c\"%%c\\00\"\n" ); + fprintf(output, "@.strprintstr = private unnamed_addr constant [3 x i8] c\"%%s\\00\"\n" ); + fprintf(output, "; End of monga dependencies \n" ); +} + +void codeTreeLLVM() { + if(output==NULL) { + defaultOutput(); + } + currentStringConstant = 0; + printf("generating code for tree\n"); + codeExtraDeclares(); + codeDefList(globalTree->next); +} +static void codeDefVar(DefVar* dv) { + + printDefVar(dv,0); + NameL* single = (NameL*)malloc(sizeof(NameL)); + single->next = NULL; + single->name = dv->id; + + fprintf(output, ";not implemented %s \n", dv->id ); + codeNameList(single,dv->t,dv->scope); + free(single); + +} +static void codeDefFunc(DefFunc* df) { + + char* typeStr = stringForType(df->retType); + if(df->b) { + currentFunctionTIndex = 0; + currentParameters = df->params; + declareTop = 0; + currentFuncHasReturn = 0; + currentBrIndex = 0; + fprintf(output, "define %s @%s(", typeStr,df->id); + codeParams(df->params); + fprintf(output, ")\n{\n"); + codeForAllocParams(df->params); + codeBlock(df->b); + if(df->retType == NULL) { + fprintf(output, "ret void\n"); + } //probably missing a ret in the end of void func + else if(currentFuncHasReturn == 0) { + fprintf(stderr, "Warning: missing return in function %s\n",df->id); + fprintf(stderr, "I will be overwritten by i deafult return\n"); + } + codeDefaultReturn(df->retType); + fprintf(output, "}\n"); + currentFunctionTIndex = 0; + currentParameters = NULL; + declateStringsToDeclare(); + currentFuncHasReturn = 0; + currentBrIndex = 0; + } + else { + fprintf(output, "declare %s @%s(", typeStr,df->id); + codeParams(df->params); + fprintf(output, ")\n"); + } +} +static void codeDefVarList(DefVarL* dvl) { + + DefVarL* p = dvl; + while(p) { + codeDefVar(p->dv); + p=p->next; + } +} +static void codeDefList(Def* d) { + if(!d) + return; + //printf("coding DefList\n"); + switch (d->tag) { + case DVar: + codeDefVar(d->u.v); + break; + case DFunc: + codeDefFunc(d->u.f); + break; + } + codeDefList(d->next); + +} +static void codeNameList(NameL* nl,Type* t,int scope) { + char* tStr = stringForType(t); + char* vStr = stringForDefaultValue(t); + NameL* p = nl; + if(scope) { + while(p) { + char* string = stringForVarAddress(p->name,scope); + fprintf(output, "%s = alloca %s\n", + string, + tStr ); + free(string); + p=p->next; + } + } else { + while(p) { + char* string = stringForVarAddress(p->name,scope); + + fprintf(output, "%s = global %s %s \n", + string, + tStr, + vStr ); + free(string); + p=p->next; + } + } +} +static void codeType(Type* t); +static void codeParams(Parameter* params) { + if(!params) + return; + char * tStr = stringForType(params->t); + fprintf(output,"%s",tStr); + if(params->next) { + fprintf(output, "," ); + codeParams(params->next); + } +} +static void codeForAllocParams(Parameter* params) { + if(!params) + return; + + int index = currentFunctionTIndex; + Parameter* p = params; + while(p) { + char * tStr = stringForType(p->t); + fprintf(output,"%%t%d = alloca %s\n", currentFunctionTIndex++, tStr); + p = p->next; + } + p = params; + int i=0; + while(p) { + char * tStr = stringForType(p->t); + fprintf(output,"store %s %%%d, %s* %%t%d\n", tStr, i++, tStr, index++); + p = p->next; + } + +} +static void codeForAssign() { + +} +/* + %t315 = getelementptr i32* @g25, i32 0 + ~= i315 = &g25[0]; +*/ +// void getelementptr(char* str) { +// fprintf(output, "%s\n", ); +// } +static char* adressOfLeftAssign(Exp* e) { + //printf("addr left assig\n"); + if(e->tag == ExpVar) { + //printf("e->var.id %s\n",e->var->id); + + if (e->var->declaration == NULL){ + Parameter* p = currentParameters; + int t=0; + while(p) { + if(strcmp(e->var->id,p->id)==0) + break; + p=p->next; + t++; + } + char * str = (char*)malloc(t/10+3); + sprintf(str,"%%t%d",t); + return str; + } + else { + int scope = e->var->declaration->scope; + char* varAddr = stringForVarAddress(e->var->id,scope); + return varAddr; + } + } + else if(e->tag == ExpAccess) { + int i1 = codeExpAccess(e)-1; //received getElemPtr + char * str = (char*)malloc(i1/10+3); + sprintf(str,"%%t%d",i1); + return str; + } + else { + fprintf(output, ";SevereError\n"); + } + return NULL; +} +static int codeCond(Exp* e) { + int i1; + //fprintf(output, ";begin codecond\n"); + i1 = codeExp(e); + + char* tStr = stringForType(e->type); + currentFunctionTIndex++; + fprintf(output, "%%t%d = icmp ne %s %%t%d, 0\n", + currentFunctionTIndex, + tStr, + i1 ); + //fprintf(output, ";end codecond\n"); + i1 = currentFunctionTIndex; + + return i1; +} +static void codeCommandList(CommandL* cl) { + if(!cl) + return; + CommandL* c = cl; + //printf("CommandL\n"); + int i1,i2; + int b1,b2,b3; + while(c) { + //printf("cl\n"); + switch(c->tag) { + case CWhile: + i1 = codeCond(c->condExp); + b1 = currentBrIndex++; + b2 = currentBrIndex++; + fprintf(output, "br i1 %%t%d, label %%b%d, label %%b%d\n", + i1, + b1, + b2); + fprintf(output, "b%d:\n",b1 ); + codeCommandList(c->cmdIf ); + i2 = codeCond(c->condExp); + fprintf(output, "br i1 %%t%d, label %%b%d, label %%b%d\n", + i2, + b1, + b2); + fprintf(output, "b%d:\n",b2 ); + // leaveScope(); + break; + case CIf: + i1 = codeCond(c->condExp); + b1 = currentBrIndex++; + b2 = currentBrIndex++; + fprintf(output, "br i1 %%t%d, label %%b%d, label %%b%d\n", + i1, + b1, + b2); + fprintf(output, "b%d:\n",b1 ); + codeCommandList(c->cmdIf ); + fprintf(output, "br label %%b%d\n",b2 ); + fprintf(output, "b%d:\n",b2 ); // leaveScope(); + break; + case CIfElse: + i1 = codeCond(c->condExp); + b1 = currentBrIndex++; + b2 = currentBrIndex++; + b3 = currentBrIndex++; + fprintf(output, "br i1 %%t%d, label %%b%d, label %%b%d\n", + i1, + b1, + b2); + fprintf(output, "b%d:\n",b1 ); + codeCommandList(c->cmdIf ); + fprintf(output, "br label %%b%d\n",b3 ); + fprintf(output, "b%d:\n",b2 ); + codeCommandList(c->cmdElse ); + fprintf(output, "br label %%b%d\n",b3 ); + fprintf(output, "b%d:\n",b3 ); + break; + case CReturn: + //printf("cret\n"); + currentFuncHasReturn = 1; + if(c->retExp == NULL) { + fprintf(output, "ret void\n"); + } + else { + char * tStr = stringForType(c->retExp->type); + i1 = codeExp(c->retExp); + fprintf(output, "ret %s %%t%d\n",tStr,i1); + } + break; + case CAssign: + i1 = codeExp(c->expRight); + char* tStr = stringForType(c->expLeft->type); + char* addr = adressOfLeftAssign(c->expLeft); + + fprintf(output, "store %s %%t%d, %s* %s \n", + tStr,i1,tStr,addr); + break; + case CBlock: + //printf("cblock\n"); + codeBlock((Block*)c->block ); + // leaveScope(); + break; + case CCall: + //printf("ccall\n"); + codeExp(c->expRight); + break; + case CPrint: + i1 = codeExp(c->printExp); + printExp(c->printExp,0); + if(c->printExp->type == NULL) { + fprintf(output, ";printing void expression is unavaible\n" ); + } + else if(c->printExp->type->tag == base) { + switch(c->printExp->type->b) { + case WInt: + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i32 %%t%d)\n", + i1 ); + + break; + case WFloat: + currentFunctionTIndex++; + fprintf(output, "%%t%d = fpext float %%t%d to double\n", + currentFunctionTIndex, + i1 ); + + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.floatprintstr, i64 0, i64 0), double %%t%d)\n", + currentFunctionTIndex ); + break; + case WChar: + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8 %%t%d)\n", + i1 ); + case WByte: + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8 %%t%d)\n", + i1 ); + break; + } + } + else if(c->printExp->type->of->tag == base && + c->printExp->type->of->b == WChar || c->printExp->type->of->b == WByte ) { + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.strprintstr, i64 0, i64 0), i8* %%t%d)\n", + i1 ); + //fprintf(output, "tail call i32 @puts(i8* %%t%d)\n", i1); + + } + else { + + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.strprintstr, i64 0, i64 0), i8* %%t%d)\n", + i1 ); + fprintf(output, ";printing non string array is unavaible\n" ); + } + // if (!checkPrintability(c->printExp)) { + // typeError("Expression is not printable"); + // } + break; + } + c = c->next; + } +} +static void codeBlock(Block* b) { + codeDefVarList(b->dvl); + codeCommandList(b->cl); +} +static int codeBinExp(Exp* e ,char * cmd) { + int te1,te2; + te1 = codeExp(e->bin.e1 ); + te2 = codeExp(e->bin.e2 ); + // te2 = currentFunctionTIndex-1; + // te1 = te2-1; + char * tStr = stringForType(e->type); + currentFunctionTIndex++; + int index = currentFunctionTIndex; + fprintf(output, "%%t%d = %s %s %%t%d, %%t%d\n", + currentFunctionTIndex++,cmd, tStr, te1,te2); + return index; +} +static int codeCallExp(Exp* e) { + int toCall = -1; + int size=0; + ExpList *p = e->call.expList; + //calculate size + while(p) { + size++; + p = p->next; + } + //generate code for arguments + p = e->call.expList; + int * args = (int*)malloc(sizeof(int)*size); + int i=0; + while(p) { + int index = codeExp(p->e); + args[i] = index; + i++; + p=p->next; + } + + if(e->type == NULL) { + fprintf(output, "call void @%s(", + e->call.id); + } + else { + char* fTypeStr = stringForType(e->type); + toCall = ++currentFunctionTIndex; + fprintf(output, "%%t%d = call %s @%s(", + toCall, + fTypeStr, + e->call.id); + } + p = e->call.expList; + i=0; + while(p) { + char* tStr = stringForType(p->e->type); + fprintf(output, "%s %%t%d",tStr,args[i]); + if(p->next) + fprintf(output, ", "); + p = p->next; + i++; + } + fprintf(output, ")\n" ); + return toCall; +} +static char* hexaStringForFloat(float c) { + double f = (double)c; + int i; + unsigned char buff[sizeof(double)]; + memcpy(buff,&f,sizeof(double)); + + char* string = (char*)malloc(sizeof(double)+3); + char temp[3] = "0x"; + strcpy(string,temp); + for(i = sizeof(double)-1;i >= 0;i--){ + sprintf(temp,"%02X",buff[i]); + strcat(string,temp); + } + return string; + +} +static char* stringForConstant(Constant* c) { + //char str[40] = "no string given"; + char* str; + double nd; + int exponent = 0; + if(!c) + return NULL; + switch(c->tag) { + case KInt: + str = (char*)malloc(40); + sprintf(str, "%d", c->u.i); + break; + case KFloat: + nd = frexp(c->u.d, &exponent); + str = hexaStringForFloat(c->u.d); + break; + case KStr: + str = (char*)c->u.str; + break; + } + return &str[0]; +} +static int codeExpPrim(Exp* e) { + currentFunctionTIndex++; + char* tStr = stringForType(e->type); + if(e->c->tag == KStr) { + currentStringConstant++; + char* cStr = stringForConstant(e->c); + int len = strlen(cStr) + 1; + fprintf(output, "%%t%d = getelementptr inbounds [%d x i8], [%d x i8]* @.cstr.%d, i64 0, i64 0\n", + currentFunctionTIndex, + len, + len, + currentStringConstant ); + + pushStringToDeclare(cStr); + + return currentFunctionTIndex; + } + + char* cStr = stringForConstant(e->c); + // fprintf(output, "%%t%d = alloca %s* \n",currentFunctionTIndex); + // fprintf(output, "store i8* %0, i8** %2, align 8\n", ); + if(e->c->tag == KFloat) { + fprintf(output, "%%t%d= fadd %s 0.0 , %s\n", + currentFunctionTIndex, + tStr, + cStr ); + } else { + fprintf(output, "%%t%d= add nsw %s 0 , %s\n", + currentFunctionTIndex, + tStr, + cStr ); + } + int index = currentFunctionTIndex++; + return index; +} +static int getAddressOfVar(Var* id) { + return -1; +} +static int codeExpVar(Exp* e) { + currentFunctionTIndex++; + char* tStr = stringForType(e->type); + if(e->var->declaration == NULL) + { + //printf(";params\n"); + Parameter* p = currentParameters; + int t=0; + while(p) { + if(strcmp(e->var->id,p->id)==0) + break; + p=p->next; + t++; + } + + fprintf(output,"%%t%d = load %s, %s* %%t%d\n", + currentFunctionTIndex, + tStr, + tStr, + t); + } + else { + int scope = e->var->declaration->scope; + if(scope == 0) { + char* varAddr = stringForVarAddress(e->var->id,scope); + fprintf(output,"%%t%d = load %s, %s* %s\n", + currentFunctionTIndex, + tStr, + tStr, + varAddr); + //fprintf(output, ";global\n"); + } + else { + char* varAddr = stringForVarAddress(e->var->id,scope); + fprintf(output,"%%t%d = load %s, %s* %s\n", + currentFunctionTIndex, + tStr, + tStr, + varAddr); + + //fprintf(output, ";scope not zero\n"); + } + //printType(e->type,0); + } + return currentFunctionTIndex; +} +static int codeExpUnary(Exp* e) { + char* tStr = stringForType(e->type); + int i1,i2; + i1 = codeExp(e->unary.e); + currentFunctionTIndex++; + switch(e->unary.op) { + case MINUS: + if(e->type->b == WFloat) { + fprintf(output, "%%t%d = fsub %s 0.0, %%t%d\n", + currentFunctionTIndex, + tStr, + i1); + + } + else { + fprintf(output, "%%t%d = sub nsw %s 0, %%t%d\n", + currentFunctionTIndex, + tStr, + i1); + } + break; + case NOT: + if(e->type->b == WFloat) { + fprintf(output, "%%t%d = fcmp oeq float %%t%d, 0.0\n", + currentFunctionTIndex, + i1); + i2 = currentFunctionTIndex++; + fprintf(output, "%%t%d = uitofp i1 %%t%d to float\n", + currentFunctionTIndex, + i2); + } + else { + fprintf(output, "%%t%d = icmp eq %s %%t%d, 0\n", + currentFunctionTIndex, + tStr, + i1); + i2 = currentFunctionTIndex++; + fprintf(output, "%%t%d = zext i1 %%t%d to %s\n", + currentFunctionTIndex, + i2, + tStr ); + } + break; + } + return currentFunctionTIndex; +} +static int codeExpCast(Exp* e) { + int i1 = codeExp(e->cast.e); + char* orTStr = stringForType(e->cast.e->type); + char* toTStr = stringForType(e->type); + currentFunctionTIndex++; + if(e->type->b == WFloat) { + fprintf(output, "%%t%d = sitofp %s %%t%d to float\n", + currentFunctionTIndex, + orTStr, + i1 ); + } + else if(e->cast.e->type->b == WFloat) { + fprintf(output, "%%t%d = fptosi float %%t%d to %s\n", + currentFunctionTIndex, + i1, + toTStr); + + } + else { + if(e->type->b == WChar) { + fprintf(output, "%%t%d = trunc i32 %%t%d to i8\n", + currentFunctionTIndex, + i1 ); + } + else if(e->cast.e->type->b == WChar) { + fprintf(output, "%%t%d = sext i8 %%t%d to i32\n", + currentFunctionTIndex, + i1 ); + } + else if(e->cast.e->type->b == e->type->b) { + fprintf(output, ";cast useless\n"); + return i1; + } + else { + fprintf(output, ";cast not implemented\n"); + return -1; + } + } + return currentFunctionTIndex; +} +static char* adressOfParameter(const char* id) { + Parameter* p = currentParameters; + int t=0; + while(p) { + if(strcmp(id,p->id)==0) + break; + p=p->next; + t++; + } + char * str = (char*)malloc(t/10+3); + sprintf(str,"%%t%d",t); + return str; + +} +static char* addressOfVector(Exp* e) { + //printf("addressOfVector\n"); + if(e->tag == ExpAccess) { + //printf("gacr\n"); + return addressOfVector(e->access.varExp); + } + else if(e->tag == ExpVar) { + //printf("gVar\n"); + + if(e->var->declaration == NULL) + { + return adressOfParameter(e->var->id); + } + return adressOfLeftAssign(e); + } + else { + return "%%SevereError"; + } +} +static int codeGetElemPtr(Type* type,int arrayTemp,int indexTemp) { + currentFunctionTIndex++; + char* tStr = stringForType(type); + fprintf(output, "%%t%d = getelementptr %s, %s* %%t%d, i64 %%t%d\n", + currentFunctionTIndex, + tStr, + tStr, + arrayTemp, + indexTemp); + return currentFunctionTIndex; +} +static int codeAccessElemPtr(Exp* e) { + //fprintf(output,";getelementptr\n"); + int i1 = codeExp(e->access.indExp); + currentFunctionTIndex++; + fprintf(output, "%%t%d = sext i32 %%t%d to i64\n", + currentFunctionTIndex, + i1 ); + + char* tStr = stringForType(e->type); + int index = currentFunctionTIndex++; + char* str = addressOfVector(e->access.varExp); + + fprintf(output, "%%t%d = load %s*, %s** %s\n", + currentFunctionTIndex, + tStr, + tStr, + str ); + //fprintf(output,"; %s mark1\n",stringForType(e->type)); + + int startArrayAddress = currentFunctionTIndex++; + fprintf(output, "%%t%d = getelementptr %s, %s* %%t%d, i64 %%t%d\n", + currentFunctionTIndex, + tStr, + tStr, + startArrayAddress, + index); + return currentFunctionTIndex; +} +static int codeExpAccess(Exp* e) { + //fprintf(output,";Exp Access\n"); + + int i1,i2; + char* tStr; + int access; + switch (e->tag) { + case ExpAccess : + i1 = codeExp(e->access.indExp); + currentFunctionTIndex++; + fprintf(output, "%%t%d = sext i32 %%t%d to i64\n", + currentFunctionTIndex, + i1 ); + tStr = stringForType(e->type); + int index = currentFunctionTIndex++; + access = codeExp(e->access.varExp); + currentFunctionTIndex++; + + i2 = codeGetElemPtr(e->type, + access, + index); + // char* str = stringForVarAddress(e->access.varExp); + currentFunctionTIndex++; + fprintf(output, "%%t%d = load %s, %s* %%t%d\n", + currentFunctionTIndex, + tStr, + tStr, + i2 ); + + return currentFunctionTIndex; + break; + default: + //fprintf(output, ";default reached\n" ); + tStr = stringForType(e->type); + char* str = addressOfVector(e); + currentFunctionTIndex++; + fprintf(output, "%%t%d = load %s, %s* %s\n", + currentFunctionTIndex, + tStr, + tStr, + str ); + return currentFunctionTIndex; + break; + } + // i1 = codeAccessElemPtr(e); + // char* tStr = stringForType(e->type); + // currentFunctionTIndex++; + // fprintf(output, "%%t%d = load %s, %s* %%t%d\n", + // currentFunctionTIndex, + // tStr, + // tStr, + // i1); + return currentFunctionTIndex; +} +static int sizeOfType(Type* t) { + if(t->tag == base) { + switch(t->b) { + case WInt: + return sizeof(int); + break; + case WFloat: + return sizeof(float); + break; + case WChar: + return sizeof(char); + break; + } + } + else { + return sizeof(int*); //pointer size + } +} +static int codeExpNew(Exp* e) { + int i1 = codeExp(e->eNew.e); + char * tStr = stringForType(e->type); + currentFunctionTIndex++; + fprintf(output, "%%t%d = mul i32 %%t%d, %d\n", + currentFunctionTIndex, + i1, + sizeOfType(e->type)); + i1 = currentFunctionTIndex++; + fprintf(output, " %%t%d = tail call i8* @malloc(i32 %%t%d)\n", + currentFunctionTIndex, + i1); + int i2 = currentFunctionTIndex++; + fprintf(output, "%%t%d = bitcast i8* %%t%d to %s\n", + currentFunctionTIndex, + i2, + tStr ); + return currentFunctionTIndex; +} +static void codeLabel(int label) { + fprintf(output, "b%d:\n",label); +} +static void codeBranches(int cond, int lt,int lf) { + fprintf(output, "br i1 %%t%d, label %%b%d, label %%b%d\n", + cond, + lt, + lf); +} + +static int codeCondToValue(int b1,int b2,int b3) { + fprintf(output, "b%d:\n", b1); + fprintf(output, "br label %%b%d\n", + b3); + fprintf(output, "b%d:\n", b2); + fprintf(output, "br label %%b%d\n", + b3); + fprintf(output, "b%d:\n", b3); + currentFunctionTIndex++; + fprintf(output, "%%t%d = phi i32 [ 1, %%b%d ], [0, %%b%d]\n", + currentFunctionTIndex, + b1, + b2 ); + return currentFunctionTIndex; +} +static int codeSimpleCompare(Exp* e,const char* oprStr ) { + int i1,i2; + i1 = codeExp(e->cmp.e1); + i2 = codeExp(e->cmp.e2); + currentFunctionTIndex++; + fprintf(output, "%%t%d = icmp %s i32 %%t%d, %%t%d\n", + currentFunctionTIndex, + oprStr, + i1, + i2 ); + return currentFunctionTIndex; + +} +static int codeExpCompare(Exp* e) { + int i1,i2; + int b1 = currentBrIndex++; + int b2 = currentBrIndex++; + int b3 = currentBrIndex++; + int ln; + switch(e->cmp.op) { + case GT: + codeSimpleCompare(e,"sgt"); + break; + case GTE: + codeSimpleCompare(e,"sge"); + break; + case LS: + codeSimpleCompare(e,"slt"); + break; + case LSE: + codeSimpleCompare(e,"sle"); + break; + case EqEq: + codeSimpleCompare(e,"eq"); + break; + case OR: + i1 = codeExp(e->cmp.e1); + currentFunctionTIndex++; + fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + currentFunctionTIndex, + i1); + ln = currentBrIndex++; + codeBranches(currentFunctionTIndex,b1,ln); + codeLabel(ln); + i2 = codeExp(e->cmp.e2); + currentFunctionTIndex++; + fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + currentFunctionTIndex, + i2); + codeBranches(currentFunctionTIndex,b1,b2); + return codeCondToValue(b1,b2,b3); + break; + case AND: + i1 = codeExp(e->cmp.e1); + currentFunctionTIndex++; + fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + currentFunctionTIndex, + i1); + ln = currentBrIndex++; + codeBranches(currentFunctionTIndex,ln,b2); + codeLabel(ln); + i2 = codeExp(e->cmp.e2); + currentFunctionTIndex++; + fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + currentFunctionTIndex, + i2); + codeBranches(currentFunctionTIndex,b1,b2); + return codeCondToValue(b1,b2,b3); + break; + } + codeBranches(currentFunctionTIndex,b1,b2); + return codeCondToValue(b1,b2,b3); +} + +static int codeExp(Exp* e) { + int result =-1; + if(!e) + return -1; + switch(e->tag) { + case ExpAdd: + if(e->type->b == WFloat) + result = codeBinExp(e,"fadd"); + else + result = codeBinExp(e,"add nsw"); + break; + case ExpSub: + if(e->type->b == WFloat) + result = codeBinExp(e,"fsub"); + else + result =codeBinExp(e,"sub nsw"); + break; + case ExpMul: + if(e->type->b == WFloat) + result = codeBinExp(e,"fmul"); + else + result = codeBinExp(e,"mul nsw"); + break; + case ExpDiv: + if(e->type->b == WFloat) + result = codeBinExp(e,"fdiv"); + else + result = codeBinExp(e,"sdiv"); + break; + case ExpCall: + result = codeCallExp(e); + break; + case ExpVar: + result = codeExpVar(e); + break; + case ExpUnary: + result = codeExpUnary(e); + break; + case ExpPrim: + result = codeExpPrim(e); + // e->type = typeOfConstant(e->c); + break; + case ExpNew: + result = codeExpNew(e); + // if(!checkTypeIndex(e->eNew.e)) { + // typeError("Index of array is not an int"); + // } + // e->type = typeOfNew(e); + break; + case ExpCmp: + result = codeExpCompare(e); + + break; + case ExpAccess: + result = codeExpAccess(e); + // typeExp(e->access.varExp); + // typeExp(e->access.indExp); + // if(!checkTypeIndex(e->access.indExp)) { + // typeError("Index of array is not an int"); + // } + // e->type = e->access.varExp->type->of; + break; + case ExpCast: + result = codeExpCast(e); + // if(!checkTypeCast(e)) { + // // printType(e->type,0); + // // printType(e->cast.type,0); + // // printType(e->cast.e->type,0); + // typeError("Cast not avaible for these types"); + // } + // e->type = e->cast.type; + break; + } + + return result; +} +static void codeVar(Var* v) { + char* tStr = stringForType(v->type); + fprintf(output, "%%L%s = alloca %s\n", + v->id, tStr ); +} +void codeConstant(Constant* c); +static void codeExpList(ExpList* el) { + char * tStr; + if(!el) + return; + ExpList *p = el; + while(p) { + int index = codeExp(p->e); + tStr = stringForType(p->e->type); + fprintf(output, "%s %%t%d",tStr,index); + if(p->next) + fprintf(output, ", "); + p = p->next; + } + return; + +} \ No newline at end of file diff --git a/src/codellvm.h b/src/codellvm.h new file mode 100644 index 0000000..3bff7ad --- /dev/null +++ b/src/codellvm.h @@ -0,0 +1,7 @@ +#include +#include +#include +#include "symbolTable.h" + +void codeTreeLLVM(); +void setCodeOutputLLVM(FILE* out); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 2bb9480..f347db7 100644 --- a/src/main.c +++ b/src/main.c @@ -44,6 +44,10 @@ #include "optimizer.h" #define optimizer_h #endif +#if !defined(codellvm_h) + #include "codellvm.h" + #define codellvm_h +#endif #include #include @@ -171,7 +175,7 @@ int main (int argc, char** argv) char noTree =0; char noChecks=0; char noCode = 0; - char noBin = 1; + char noBin = 0; char noDebug = 0; char* option = NULL; @@ -301,7 +305,7 @@ int main (int argc, char** argv) printTree(); } char * bf_name = "a.bf"; - //char * bin_name = "a.out"; + char * bin_name = "a.ll"; if(!noCode) { FILE* bf_location = fopen(bf_name,"wt"); setCodeOutput(bf_location); @@ -312,14 +316,19 @@ int main (int argc, char** argv) } if(!noBin) { + FILE* bin_location = fopen(bin_name,"wt"); + setCodeOutputLLVM(bin_location); + codeTreeLLVM(); + fclose(bin_location); + char* str = handleClangOptions(argc,argv); char* buff = (char*)malloc( strlen(str) + strlen("clang") + - strlen(bf_name)+1); + strlen(bin_name)+1); sprintf(buff,"clang %s %s", str, - bf_name); + bin_name); int s = system(buff); return s; } From d7d6578727ddfccf55effa2f75518c46e3c70452 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Mon, 27 Sep 2021 10:11:27 +0100 Subject: [PATCH 02/20] type fixes --- src/codellvm.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index ef403bc..e0b00c2 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -49,6 +49,7 @@ static void defaultOutput() { static Parameter* currentParameters = NULL; char* stringForType(Type* t) { + printType(t,0); char* tStr = NULL; if(t == NULL) return "void"; @@ -65,7 +66,7 @@ char* stringForType(Type* t) { return "i8"; break; case WByte: - return "i32"; + return "i8"; case WShort: return "i16"; } @@ -158,6 +159,7 @@ static void codeExtraDeclares() { fprintf(output, "declare i8* @malloc(i32)\n" ); //malloc fprintf(output, "declare i32 @printf(i8* nocapture readonly, ...)\n" ); fprintf(output, "declare i32 @puts(i8* nocapture readonly)\n" ); + fprintf(output, "declare i32 @scanf(i8*, ...)\n" ); fprintf(output, "@.intprintstr = private unnamed_addr constant [3 x i8] c\"%%d\\00\"\n" ); fprintf(output, "@.floatprintstr = private unnamed_addr constant [3 x i8] c\"%%f\\00\"\n" ); fprintf(output, "@.charprintstr = private unnamed_addr constant [3 x i8] c\"%%c\\00\"\n" ); @@ -449,6 +451,11 @@ static void codeCommandList(CommandL* cl) { //printf("ccall\n"); codeExp(c->expRight); break; + case CRead: + i1 = codeExp(c->printExp); + // We have to get the address of the var, to scanf to there + fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8* %%t%d)\n",i1); + case CPrint: i1 = codeExp(c->printExp); printExp(c->printExp,0); @@ -717,6 +724,7 @@ static int codeExpUnary(Exp* e) { i2); } else { + fprintf(output, "%%t%d = icmp eq %s %%t%d, 0\n", currentFunctionTIndex, tStr, @@ -908,6 +916,8 @@ static int sizeOfType(Type* t) { break; case WChar: return sizeof(char); + case WByte: + return sizeof(char); break; } } @@ -961,12 +971,20 @@ static int codeCondToValue(int b1,int b2,int b3) { } static int codeSimpleCompare(Exp* e,const char* oprStr ) { int i1,i2; + const char* tStr = stringForType(e->cmp.e1->type); //Consider undoing symboltable autocast to int i1 = codeExp(e->cmp.e1); i2 = codeExp(e->cmp.e2); currentFunctionTIndex++; - fprintf(output, "%%t%d = icmp %s i32 %%t%d, %%t%d\n", + printf("%%t%d = icmp %s %s %%t%d, %%t%d\n", currentFunctionTIndex, oprStr, + tStr, + i1, + i2 ); + fprintf(output, "%%t%d = icmp %s %s %%t%d, %%t%d\n", + currentFunctionTIndex, + oprStr, + tStr, i1, i2 ); return currentFunctionTIndex; @@ -978,6 +996,7 @@ static int codeExpCompare(Exp* e) { int b2 = currentBrIndex++; int b3 = currentBrIndex++; int ln; + const char* tStr = stringForType(e->type); switch(e->cmp.op) { case GT: codeSimpleCompare(e,"sgt"); @@ -997,7 +1016,8 @@ static int codeExpCompare(Exp* e) { case OR: i1 = codeExp(e->cmp.e1); currentFunctionTIndex++; - fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + fprintf(output, "%%t%d = icmp ne %s %%t%d, 0\n", + tStr, currentFunctionTIndex, i1); ln = currentBrIndex++; @@ -1005,7 +1025,8 @@ static int codeExpCompare(Exp* e) { codeLabel(ln); i2 = codeExp(e->cmp.e2); currentFunctionTIndex++; - fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + fprintf(output, "%%t%d = icmp ne %s %%t%d, 0\n", + tStr, currentFunctionTIndex, i2); codeBranches(currentFunctionTIndex,b1,b2); @@ -1014,16 +1035,18 @@ static int codeExpCompare(Exp* e) { case AND: i1 = codeExp(e->cmp.e1); currentFunctionTIndex++; - fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + fprintf(output, "%%t%d = icmp ne %s %%t%d, 0\n", currentFunctionTIndex, + tStr, i1); ln = currentBrIndex++; codeBranches(currentFunctionTIndex,ln,b2); codeLabel(ln); i2 = codeExp(e->cmp.e2); currentFunctionTIndex++; - fprintf(output, "%%t%d = icmp ne i32 %%t%d, 0\n", + fprintf(output, "%%t%d = icmp ne %s %%t%d, 0\n", currentFunctionTIndex, + tStr, i2); codeBranches(currentFunctionTIndex,b1,b2); return codeCondToValue(b1,b2,b3); From 482964c8d7e3f7f7f0f020f38e2c0316f9030942 Mon Sep 17 00:00:00 2001 From: lmos Date: Mon, 27 Sep 2021 14:41:54 +0100 Subject: [PATCH 03/20] CRead --- src/codellvm.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/codellvm.c b/src/codellvm.c index e0b00c2..91d4adc 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -452,10 +452,20 @@ static void codeCommandList(CommandL* cl) { codeExp(c->expRight); break; case CRead: +<<<<<<< Updated upstream i1 = codeExp(c->printExp); // We have to get the address of the var, to scanf to there fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8* %%t%d)\n",i1); +======= + tStr = stringForType(c->printExp->type); + addr = adressOfLeftAssign(c->printExp); + fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %s* %s)", + tStr, + addr); + + break; +>>>>>>> Stashed changes case CPrint: i1 = codeExp(c->printExp); printExp(c->printExp,0); From 5c45588a68f04f9bf2fbe2cb7ff9dbe22f5b9a9b Mon Sep 17 00:00:00 2001 From: lmos Date: Mon, 27 Sep 2021 14:42:14 +0100 Subject: [PATCH 04/20] more for CRead --- src/codellvm.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index 91d4adc..9af99a0 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -92,7 +92,7 @@ static char* stringForDefaultValue(Type* t) { return "void"; } if(t->tag == base) { - if(t->b == WInt || t->b == WChar || t->b == WByte) { + if(t->b == WInt || t->b == WChar || t->b == WByte || t->b == WShort) { return "0"; } else { @@ -164,6 +164,9 @@ static void codeExtraDeclares() { fprintf(output, "@.floatprintstr = private unnamed_addr constant [3 x i8] c\"%%f\\00\"\n" ); fprintf(output, "@.charprintstr = private unnamed_addr constant [3 x i8] c\"%%c\\00\"\n" ); fprintf(output, "@.strprintstr = private unnamed_addr constant [3 x i8] c\"%%s\\00\"\n" ); + fprintf(output, "@.charreadstr = private unnamed_addr constant [4 x i8] c\" %%c\\00\"\n" ); + fprintf(output, "@.intreadstr = private unnamed_addr constant [4 x i8] c\" %%d\\00\"\n" ); + fprintf(output, "@.floatreadstr = private unnamed_addr constant [4 x i8] c\" %%f\\00\"\n" ); fprintf(output, "; End of monga dependencies \n" ); } @@ -452,20 +455,39 @@ static void codeCommandList(CommandL* cl) { codeExp(c->expRight); break; case CRead: -<<<<<<< Updated upstream - i1 = codeExp(c->printExp); - // We have to get the address of the var, to scanf to there - fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8* %%t%d)\n",i1); - -======= tStr = stringForType(c->printExp->type); addr = adressOfLeftAssign(c->printExp); - fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %s* %s)", - tStr, - addr); + fprintf(output, ";tStr %s\n", tStr); + fprintf(output,";addr %s\n", addr); + if(c->printExp->type == NULL) { + fprintf(output, ";printing void expression is unavaible\n" ); + } + else if(c->printExp->type->tag == base) { + switch(c->printExp->type->b) { + case WChar: + fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.charreadstr, i64 0, i64 0), %s* %s)\n", + tStr, + addr); + break; + case WInt: + fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.intreadstr, i64 0, i64 0), %s* %s)\n", + tStr, + addr); + break; + case WByte: + fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.intreadstr, i64 0, i64 0), %s* %s)\n", + tStr, + addr); + break; + } + } else { + fprintf(output, "PIROCA, %s* %s)\n", + tStr, + addr); + } + break; ->>>>>>> Stashed changes case CPrint: i1 = codeExp(c->printExp); printExp(c->printExp,0); @@ -492,7 +514,7 @@ static void codeCommandList(CommandL* cl) { fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8 %%t%d)\n", i1 ); case WByte: - fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8 %%t%d)\n", + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i8 %%t%d)\n", i1 ); break; } From 5a5cb30c8084dc81ad81e550d3961f07246cdba0 Mon Sep 17 00:00:00 2001 From: lmos Date: Mon, 27 Sep 2021 15:48:07 +0100 Subject: [PATCH 05/20] Better error message --- src/codellvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codellvm.c b/src/codellvm.c index 9af99a0..396d7e1 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -481,7 +481,7 @@ static void codeCommandList(CommandL* cl) { break; } } else { - fprintf(output, "PIROCA, %s* %s)\n", + fprintf(output, ";Unknown Error on Read. Not Implemented, %s* %s)\n", tStr, addr); } From 8b3b624a3dd37dcec6b567d3808269845f45d70f Mon Sep 17 00:00:00 2001 From: lmos Date: Mon, 27 Sep 2021 18:05:20 +0100 Subject: [PATCH 06/20] WShort adds, bugfixes and retranslatescape --- src/codellvm.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index 396d7e1..e1024dc 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -25,6 +25,7 @@ static void codeConstant(Constant* c); static void codeExpList(ExpList* el); static int codeAccessElemPtr(Exp* e); static char* stringForType(Type* t); +static char* retranslatescape(const char* scape); static FILE* output = NULL; static int currentFunctionTIndex = 0; @@ -474,6 +475,11 @@ static void codeCommandList(CommandL* cl) { tStr, addr); break; + case WShort: + fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.intreadstr, i64 0, i64 0), %s* %s)\n", + tStr, + addr); + break; case WByte: fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.intreadstr, i64 0, i64 0), %s* %s)\n", tStr, @@ -490,6 +496,7 @@ static void codeCommandList(CommandL* cl) { break; case CPrint: i1 = codeExp(c->printExp); + printf("; printexp i1 %d", i1); printExp(c->printExp,0); if(c->printExp->type == NULL) { fprintf(output, ";printing void expression is unavaible\n" ); @@ -499,7 +506,10 @@ static void codeCommandList(CommandL* cl) { case WInt: fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i32 %%t%d)\n", i1 ); - + break; + case WShort: + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i16 %%t%d)\n", + i1 ); break; case WFloat: currentFunctionTIndex++; @@ -513,6 +523,7 @@ static void codeCommandList(CommandL* cl) { case WChar: fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8 %%t%d)\n", i1 ); + break; case WByte: fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i8 %%t%d)\n", i1 ); @@ -635,7 +646,7 @@ static char* stringForConstant(Constant* c) { str = hexaStringForFloat(c->u.d); break; case KStr: - str = (char*)c->u.str; + str = (char*)c->u.str; break; } return &str[0]; @@ -825,6 +836,35 @@ static char* adressOfParameter(const char* id) { return str; } +static char* retranslatescape(const char* scape) { + int len = strlen(scape); + char* result = (char*)malloc(len*2); + char* smallStr; + int i,j; + for(i=0,j=0; itag == ExpAccess) { From 0d808d74875f03d0966588cc415252b1cd581ac6 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Tue, 28 Sep 2021 21:09:36 +0100 Subject: [PATCH 07/20] solve some warnings --- src/codellvm.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index e1024dc..75686f3 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -21,7 +21,7 @@ static int codeExpAccess(Exp* e); static void codeBlock(Block* b); static int codeExp(Exp* e); static void codeVar(Var* v); -static void codeConstant(Constant* c); +//static void codeConstant(Constant* c); static void codeExpList(ExpList* el); static int codeAccessElemPtr(Exp* e); static char* stringForType(Type* t); @@ -70,6 +70,8 @@ char* stringForType(Type* t) { return "i8"; case WShort: return "i16"; + case WBit: + return "i1"; } break; case array: @@ -85,6 +87,7 @@ char* stringForType(Type* t) { break; } + return "Unknown Type Error"; } static char* stringForDefaultValue(Type* t) { @@ -241,7 +244,7 @@ static void codeDefList(Def* d) { //printf("coding DefList\n"); switch (d->tag) { case DVar: - codeDefVar(d->u.v); + codeDefVarList(d->u.v); break; case DFunc: codeDefFunc(d->u.f); @@ -455,6 +458,15 @@ static void codeCommandList(CommandL* cl) { //printf("ccall\n"); codeExp(c->expRight); break; + case COperator: + fprintf(output, ";COperator not implemented\n"); + break; + case CDebug: + fprintf(output, ";CDebug not implemented\n"); + break; + case CFor: + fprintf(output, ";CFor not implemented\n"); + break; case CRead: tStr = stringForType(c->printExp->type); addr = adressOfLeftAssign(c->printExp); @@ -485,6 +497,9 @@ static void codeCommandList(CommandL* cl) { tStr, addr); break; + default: + fprintf(output, "%s\n", "not implemented" ); + break; } } else { fprintf(output, ";Unknown Error on Read. Not Implemented, %s* %s)\n", @@ -528,10 +543,13 @@ static void codeCommandList(CommandL* cl) { fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i8 %%t%d)\n", i1 ); break; + default: + fprintf(output, "%s\n", ";not implemented"); + break; } } else if(c->printExp->type->of->tag == base && - c->printExp->type->of->b == WChar || c->printExp->type->of->b == WByte ) { + (c->printExp->type->of->b == WChar || c->printExp->type->of->b == WByte) ) { fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.strprintstr, i64 0, i64 0), i8* %%t%d)\n", i1 ); //fprintf(output, "tail call i32 @puts(i8* %%t%d)\n", i1); @@ -648,6 +666,9 @@ static char* stringForConstant(Constant* c) { case KStr: str = (char*)c->u.str; break; + case KBit: + str = c->u.i ? "true" : "false"; + break; } return &str[0]; } @@ -990,6 +1011,10 @@ static int sizeOfType(Type* t) { return sizeof(char); case WByte: return sizeof(char); + case WBit: + return 1; + case WShort: + return sizeof(short); break; } } @@ -1089,8 +1114,8 @@ static int codeExpCompare(Exp* e) { i1 = codeExp(e->cmp.e1); currentFunctionTIndex++; fprintf(output, "%%t%d = icmp ne %s %%t%d, 0\n", - tStr, currentFunctionTIndex, + tStr, i1); ln = currentBrIndex++; codeBranches(currentFunctionTIndex,b1,ln); @@ -1098,8 +1123,8 @@ static int codeExpCompare(Exp* e) { i2 = codeExp(e->cmp.e2); currentFunctionTIndex++; fprintf(output, "%%t%d = icmp ne %s %%t%d, 0\n", + currentFunctionTIndex, tStr, - currentFunctionTIndex, i2); codeBranches(currentFunctionTIndex,b1,b2); return codeCondToValue(b1,b2,b3); @@ -1166,6 +1191,11 @@ static int codeExp(Exp* e) { case ExpUnary: result = codeExpUnary(e); break; + case ExpOperator: + //Not Implemented; + //result = codeExpPrim(e); + // e->type = typeOfConstant(e->c); + break; case ExpPrim: result = codeExpPrim(e); // e->type = typeOfConstant(e->c); From 2d79e3713f34e53b1f4bd6b4f8c37ded0ebdaaf8 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Sat, 2 Oct 2021 12:05:53 +0100 Subject: [PATCH 08/20] Operator implemented LLVM --- src/codellvm.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/codellvm.c b/src/codellvm.c index 75686f3..b2bf869 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -22,6 +22,7 @@ static void codeBlock(Block* b); static int codeExp(Exp* e); static void codeVar(Var* v); //static void codeConstant(Constant* c); +static int codeExpOperator(Exp* e); static void codeExpList(ExpList* el); static int codeAccessElemPtr(Exp* e); static char* stringForType(Type* t); @@ -459,7 +460,8 @@ static void codeCommandList(CommandL* cl) { codeExp(c->expRight); break; case COperator: - fprintf(output, ";COperator not implemented\n"); + codeExp(c->oprExp); + //fprintf(output, ";COperator not implemented\n"); break; case CDebug: fprintf(output, ";CDebug not implemented\n"); @@ -1153,6 +1155,37 @@ static int codeExpCompare(Exp* e) { return codeCondToValue(b1,b2,b3); } +static int codeExpOperator(Exp* e){ + printf("Enter code exp ExpOperator\n"); + + if(!e){ + printf("null\n"); + return currentFunctionTIndex; + } + printExp(e,3); + + int i1 = codeExp(e->opr.e); + printf("get i1 %d\n",i1); + char* tStr = stringForType(e->opr.e->type); + printf("get tStr %s\n",tStr); + char* addr = adressOfLeftAssign(e->opr.e); + printf("get addr %s\n",addr); + currentFunctionTIndex++; + switch(e->opr.op) { + case INC: + fprintf(output, "%%t%d = add nsw %s %%t%d, %d\n", + currentFunctionTIndex, tStr, i1, e->opr.amount ); + fprintf(output, "store %s %%t%d, %s* %s ; align 4\n", tStr, currentFunctionTIndex, tStr, addr); + break; + case DEC: + fprintf(output, "%%t%d = add nsw %s %%t%d, -%d\n", + currentFunctionTIndex, tStr, i1, e->opr.amount ); + fprintf(output, "store %s %%t%d, %s* %s ; align 4\n", tStr, currentFunctionTIndex, tStr, addr); + break; + } + return currentFunctionTIndex; +} + static int codeExp(Exp* e) { int result =-1; if(!e) @@ -1192,6 +1225,7 @@ static int codeExp(Exp* e) { result = codeExpUnary(e); break; case ExpOperator: + result = codeExpOperator(e); //Not Implemented; //result = codeExpPrim(e); // e->type = typeOfConstant(e->c); From e2124444f1c179d222018e934529cd2fe71cefb7 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Sat, 2 Oct 2021 13:51:19 +0100 Subject: [PATCH 09/20] Rewrote cast and attribution --- src/codellvm.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index b2bf869..fb8bc57 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -7,6 +7,10 @@ #include "tree.h" #define tree_h #endif +#if !defined(symbolTable_h) + #include "symbolTable.h" + #define symbolTable_h +#endif static void codeDefVar(DefVar* dv); static void codeDefFunc(DefFunc* df); @@ -160,7 +164,8 @@ char* stringForVarAddress(const char* name,int scope) { } static void codeExtraDeclares() { //fprintf(output, "declare noalias i8* @malloc(i64)\n" ); - fprintf(output, "; generated with mongaComp \n" ); + fprintf(output, ";Generated by HAC-LLVM (Headache Awesome Compiler - LLVM Backend)\n" ); + fprintf(output, "; Powered by MongaComp\n" ); fprintf(output, "declare i8* @malloc(i32)\n" ); //malloc fprintf(output, "declare i32 @printf(i8* nocapture readonly, ...)\n" ); fprintf(output, "declare i32 @puts(i8* nocapture readonly)\n" ); @@ -805,7 +810,13 @@ static int codeExpUnary(Exp* e) { } return currentFunctionTIndex; } +static int typeCompare(Type* a, Type* b){ + int aSize = cellsForType(a); + int bSize = cellsForType(b); + return a - b; +} static int codeExpCast(Exp* e) { + printf("expCast\n"); int i1 = codeExp(e->cast.e); char* orTStr = stringForType(e->cast.e->type); char* toTStr = stringForType(e->type); @@ -824,22 +835,28 @@ static int codeExpCast(Exp* e) { } else { - if(e->type->b == WChar) { - fprintf(output, "%%t%d = trunc i32 %%t%d to i8\n", + int cmp = typeCompare(e->cast.e->type,e->type); + if(cmp > 0) { //sext + fprintf(output, "%%t%d = sext %s %%t%d to %s\n", currentFunctionTIndex, - i1 ); - } - else if(e->cast.e->type->b == WChar) { - fprintf(output, "%%t%d = sext i8 %%t%d to i32\n", + orTStr, + i1, + toTStr + ); + } else if(cmp < 0) { + fprintf(output, "%%t%d = trunc %s %%t%d to %s\n", currentFunctionTIndex, - i1 ); + orTStr, + i1, + toTStr ); } else if(e->cast.e->type->b == e->type->b) { fprintf(output, ";cast useless\n"); return i1; } else { - fprintf(output, ";cast not implemented\n"); + printf("Unknown cast: %s to %s \n", orTStr,toTStr); + fprintf(output, ";cast (%s to %s) not implemented\n", orTStr,toTStr); return -1; } } From 4b65853142a828b43932210ae6f21e20016c8aa2 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Sat, 2 Oct 2021 13:52:29 +0100 Subject: [PATCH 10/20] Fix typo in attribution --- src/codellvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codellvm.c b/src/codellvm.c index fb8bc57..56eda10 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -164,7 +164,7 @@ char* stringForVarAddress(const char* name,int scope) { } static void codeExtraDeclares() { //fprintf(output, "declare noalias i8* @malloc(i64)\n" ); - fprintf(output, ";Generated by HAC-LLVM (Headache Awesome Compiler - LLVM Backend)\n" ); + fprintf(output, "; Generated by HAC-LLVM (Headache Awesome Compiler - LLVM Backend)\n" ); fprintf(output, "; Powered by MongaComp\n" ); fprintf(output, "declare i8* @malloc(i32)\n" ); //malloc fprintf(output, "declare i32 @printf(i8* nocapture readonly, ...)\n" ); From 9041fa658ae49a238c1adfb160db755e5a96fde7 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Sat, 2 Oct 2021 14:35:12 +0100 Subject: [PATCH 11/20] Fix Cast --- src/codellvm.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index 56eda10..4de2b05 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -813,7 +813,12 @@ static int codeExpUnary(Exp* e) { static int typeCompare(Type* a, Type* b){ int aSize = cellsForType(a); int bSize = cellsForType(b); - return a - b; + int result = aSize - bSize; + printf("(a-b) = %d - %d = %d\n", + aSize, + bSize, + result ); + return result; } static int codeExpCast(Exp* e) { printf("expCast\n"); @@ -836,19 +841,24 @@ static int codeExpCast(Exp* e) { } else { int cmp = typeCompare(e->cast.e->type,e->type); - if(cmp > 0) { //sext + fprintf(output, ";typecmp = %d\n",cmp); + fprintf(output,";cast: %s to %s \n", orTStr,toTStr); + if(cmp < 0) { //sext fprintf(output, "%%t%d = sext %s %%t%d to %s\n", currentFunctionTIndex, orTStr, i1, toTStr ); - } else if(cmp < 0) { + } else if(cmp > 0) { fprintf(output, "%%t%d = trunc %s %%t%d to %s\n", currentFunctionTIndex, orTStr, i1, toTStr ); + } else if(cmp == 0){ + fprintf(output, ";cast useless\n"); + return i1; } else if(e->cast.e->type->b == e->type->b) { fprintf(output, ";cast useless\n"); From e1d375aa79b01e08e91cf6211f92081dd6906264 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Sat, 2 Oct 2021 15:55:19 +0100 Subject: [PATCH 12/20] Fix Behavior --- src/main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index f347db7..1fd2be1 100644 --- a/src/main.c +++ b/src/main.c @@ -92,7 +92,7 @@ static char* breakingOptions[] = { }; static char breakingOptionsCount = 3; -static char hacVersion[] = "v0.70.3b"; +static char hacVersion[] = "v0.71.3b (LLVM)"; static int isOption(const char* candidate){ for (int i=0;i Date: Sat, 16 Apr 2022 14:35:26 +0100 Subject: [PATCH 13/20] fix read and print shorts --- src/codellvm.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index 4de2b05..f7ab0ed 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -170,12 +170,15 @@ static void codeExtraDeclares() { fprintf(output, "declare i32 @printf(i8* nocapture readonly, ...)\n" ); fprintf(output, "declare i32 @puts(i8* nocapture readonly)\n" ); fprintf(output, "declare i32 @scanf(i8*, ...)\n" ); + fprintf(output, "declare i32 @putchar(i32) \n"); fprintf(output, "@.intprintstr = private unnamed_addr constant [3 x i8] c\"%%d\\00\"\n" ); + fprintf(output, "@.shortprintstr = private unnamed_addr constant [4 x i8] c\"%%hd\\00\"\n" ); fprintf(output, "@.floatprintstr = private unnamed_addr constant [3 x i8] c\"%%f\\00\"\n" ); fprintf(output, "@.charprintstr = private unnamed_addr constant [3 x i8] c\"%%c\\00\"\n" ); fprintf(output, "@.strprintstr = private unnamed_addr constant [3 x i8] c\"%%s\\00\"\n" ); fprintf(output, "@.charreadstr = private unnamed_addr constant [4 x i8] c\" %%c\\00\"\n" ); fprintf(output, "@.intreadstr = private unnamed_addr constant [4 x i8] c\" %%d\\00\"\n" ); + fprintf(output, "@.shortreadstr = private unnamed_addr constant [5 x i8] c\" %%hd\\00\"\n" ); fprintf(output, "@.floatreadstr = private unnamed_addr constant [4 x i8] c\" %%f\\00\"\n" ); fprintf(output, "; End of monga dependencies \n" ); } @@ -220,7 +223,7 @@ static void codeDefFunc(DefFunc* df) { } //probably missing a ret in the end of void func else if(currentFuncHasReturn == 0) { fprintf(stderr, "Warning: missing return in function %s\n",df->id); - fprintf(stderr, "I will be overwritten by i deafult return\n"); + fprintf(stderr, "It will be overwritten by the default return\n"); } codeDefaultReturn(df->retType); fprintf(output, "}\n"); @@ -495,7 +498,7 @@ static void codeCommandList(CommandL* cl) { addr); break; case WShort: - fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.intreadstr, i64 0, i64 0), %s* %s)\n", + fprintf(output, "call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.shortreadstr, i64 0, i64 0), %s* %s)\n", tStr, addr); break; @@ -530,7 +533,7 @@ static void codeCommandList(CommandL* cl) { i1 ); break; case WShort: - fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i16 %%t%d)\n", + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.shortprintstr, i64 0, i64 0), i16 %%t%d)\n", i1 ); break; case WFloat: From f2d3ba7a8207307899fc27ce9df255e23dc72f78 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Tue, 19 Apr 2022 20:43:10 +0100 Subject: [PATCH 14/20] change to unsigned --- src/codellvm.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index f7ab0ed..b4a31ce 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -171,12 +171,14 @@ static void codeExtraDeclares() { fprintf(output, "declare i32 @puts(i8* nocapture readonly)\n" ); fprintf(output, "declare i32 @scanf(i8*, ...)\n" ); fprintf(output, "declare i32 @putchar(i32) \n"); - fprintf(output, "@.intprintstr = private unnamed_addr constant [3 x i8] c\"%%d\\00\"\n" ); - fprintf(output, "@.shortprintstr = private unnamed_addr constant [4 x i8] c\"%%hd\\00\"\n" ); + fprintf(output, "@.intprintstr = private unnamed_addr constant [3 x i8] c\"%%u\\00\"\n" ); + fprintf(output, "@.shortprintstr = private unnamed_addr constant [4 x i8] c\"%%hu\\00\"\n" ); fprintf(output, "@.floatprintstr = private unnamed_addr constant [3 x i8] c\"%%f\\00\"\n" ); fprintf(output, "@.charprintstr = private unnamed_addr constant [3 x i8] c\"%%c\\00\"\n" ); + fprintf(output, "@.byteprintstr = private unnamed_addr constant [5 x i8] c\"%%hhu\\00\"\n" ); fprintf(output, "@.strprintstr = private unnamed_addr constant [3 x i8] c\"%%s\\00\"\n" ); fprintf(output, "@.charreadstr = private unnamed_addr constant [4 x i8] c\" %%c\\00\"\n" ); + fprintf(output, "@.bytereadstr = private unnamed_addr constant [5 x i8] c\"%%hhu\\00\"\n" ); fprintf(output, "@.intreadstr = private unnamed_addr constant [4 x i8] c\" %%d\\00\"\n" ); fprintf(output, "@.shortreadstr = private unnamed_addr constant [5 x i8] c\" %%hd\\00\"\n" ); fprintf(output, "@.floatreadstr = private unnamed_addr constant [4 x i8] c\" %%f\\00\"\n" ); @@ -536,6 +538,10 @@ static void codeCommandList(CommandL* cl) { fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.shortprintstr, i64 0, i64 0), i16 %%t%d)\n", i1 ); break; + case WByte: + fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.byteprintstr, i64 0, i64 0), i8 %%t%d)\n", + i1 ); + break; case WFloat: currentFunctionTIndex++; fprintf(output, "%%t%d = fpext float %%t%d to double\n", @@ -549,10 +555,7 @@ static void codeCommandList(CommandL* cl) { fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.charprintstr, i64 0, i64 0), i8 %%t%d)\n", i1 ); break; - case WByte: - fprintf(output, "tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.intprintstr, i64 0, i64 0), i8 %%t%d)\n", - i1 ); - break; + default: fprintf(output, "%s\n", ";not implemented"); break; From 40e0c9f386b7eb8b003ec98f72459f3d3076002c Mon Sep 17 00:00:00 2001 From: LucasMW Date: Wed, 24 Aug 2022 17:41:21 +0100 Subject: [PATCH 15/20] make static so emcc can work --- src/codeGen.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/codeGen.c b/src/codeGen.c index 80e2324..269a395 100644 --- a/src/codeGen.c +++ b/src/codeGen.c @@ -70,7 +70,7 @@ static int currentBrIndex = 0; static int currentStringConstant = 0; static int declareTop = 0; -char* stringsToDeclare[100]; +static char* stringsToDeclare[100]; extern char forceExpand; @@ -669,6 +669,8 @@ static void codeCellValuePrintAnySize(int start, int end){ popCells(10); } +void outputToBuffer(){} + void setCodeOutput(FILE* out) { setOutput(out); From b10cd989bbc9fc4799b47f82436c83ac8da9903e Mon Sep 17 00:00:00 2001 From: LucasMW Date: Wed, 24 Aug 2022 17:42:07 +0100 Subject: [PATCH 16/20] make static so emcc can work plus typo --- src/codellvm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/codellvm.c b/src/codellvm.c index b4a31ce..f034724 100644 --- a/src/codellvm.c +++ b/src/codellvm.c @@ -38,7 +38,7 @@ static int currentBrIndex = 0; static int currentStringConstant = 0; static int declareTop = 0; -char* stringsToDeclare[100]; +static char* stringsToDeclare[100]; static int currentFuncHasReturn = 0; @@ -148,7 +148,7 @@ static void declateStringsToDeclare() { char* stringForVarAddress(const char* name,int scope) { char string[50] = "no string yet"; if(strlen(name) >= 50) { - printf("SevereError. var name is to big\n"); + printf("SevereError. var name is too big\n"); } if(scope == 0) { sprintf(string,"@g%s",name); From 9fc1fc474b0e539ea36ba47d0defa7c072157f01 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Wed, 24 Aug 2022 18:11:16 +0100 Subject: [PATCH 17/20] typo and promote_type --- src/symbolTable.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/symbolTable.c b/src/symbolTable.c index 0a24a8e..d65f0dc 100644 --- a/src/symbolTable.c +++ b/src/symbolTable.c @@ -83,7 +83,7 @@ void raiseError(const char* message,int line) { exit(01); } void raiseWarning(const char* message,int line) { - printf("Warning: %s\n# near line %d",message,line); + printf("Warning: %s\n# near line %d\n",message,line); warningCount++; } void generateStardardDeclares(progNode* prog) { @@ -514,6 +514,15 @@ void promoteType(Exp ** eptr) { performCastToType(t,eptr); } } +void promoteToType(Exp ** eptr, Type* t0) { + Exp* e = *eptr; + if(e->type->tag == base && e->type->b == WByte) { + Type* t = (Type*)malloc(sizeof(Type)); + t->tag = t0->tag; + t->b = t0->b; + performCastToType(t,eptr); + } +} BType BTypeOfArith(Exp* e1,Exp *e2) { if(e1->type->b == WFloat || e2->type->b == WFloat) @@ -809,11 +818,14 @@ void typeExp(Exp* e ) { typeExp(e->cmp.e2); //promoteType(&e->cmp.e1); //promoteType(&e->cmp.e2); + switch(e->cmp.op) { default: if(!checkTypeLogic(e->cmp.e1) || !checkTypeLogic(e->cmp.e2)) { raiseError("Types not suitble for logic",e->dbg_line); + } else { + promoteToType(&e->cmp.e2,e->cmp.e1->type); } break; case EqEq: @@ -822,6 +834,8 @@ void typeExp(Exp* e ) { // printType(e->cmp.e1->type,0); // printType(e->cmp.e2->type,0); raiseError("Not comparable types in ==",e->dbg_line); + } else { + promoteToType(&e->cmp.e2,e->cmp.e1->type); } } break; From 0eb82b7a0ff1811ec07aaa683b59995972a9293f Mon Sep 17 00:00:00 2001 From: LucasMW Date: Wed, 24 Aug 2022 21:56:15 +0100 Subject: [PATCH 18/20] some tweaks on examples --- examples/calculator.ha | 2 +- examples/collatz.ha | 32 ++++++++++++++++++++++++++++++++ examples/countext.ha | 6 ++++-- examples/countext2.ha | 13 +++++++++++++ examples/example.ha | 2 +- examples/experimental/sincos.ha | 2 +- examples/factor.ha | 28 ++++++++++++++++++++++++++++ examples/factorial.ha | 5 ++--- examples/masoque.ha | 2 +- 9 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 examples/collatz.ha create mode 100644 examples/countext2.ha create mode 100644 examples/factor.ha diff --git a/examples/calculator.ha b/examples/calculator.ha index fb81872..286ae5b 100644 --- a/examples/calculator.ha +++ b/examples/calculator.ha @@ -3,7 +3,7 @@ //Interactive Calculator program void main() { char operator; - byte a,b; + short a,b; print "Enter first number:\n"; read a; print "Received number "; print a; print "\n"; diff --git a/examples/collatz.ha b/examples/collatz.ha new file mode 100644 index 0000000..25a2ccd --- /dev/null +++ b/examples/collatz.ha @@ -0,0 +1,32 @@ +short mod2(short a){ + short r,q; + q = a/(2 as short); + r = a - (2 as short)*q; + print "(a,2)"; @"= ("; @a; @","; @2; @")"; + print "(r,q)"; @"= ("; @r; @","; @q; @")"; + return r; +} + +void main() { + short x,y,z; + print "Enter a number:\n"; + + read x; + print "got "; @x; @" \n"; + y = x - (1 as short); + while(y){ + z = mod2(x); + print "z is "; @z; @"\n"; + if(z == 0) { + print "even\n"; + x = x/(2 as short); + } + else { + print "odd\n"; + x = (3 as short)*x+(1 as short); + } + print "=> "; @x; @"\n"; + y--; + + } +} \ No newline at end of file diff --git a/examples/countext.ha b/examples/countext.ha index dea8908..71f736f 100644 --- a/examples/countext.ha +++ b/examples/countext.ha @@ -3,9 +3,11 @@ void main() { @"Enter an limit\n"; #end; @"Start:\n"; - for(i=0;i<=end;i++){ + i=0; + while(i<=end){ @i;@"\n"; + i++; } - //@"End\n"; + @"End\n"; } \ No newline at end of file diff --git a/examples/countext2.ha b/examples/countext2.ha new file mode 100644 index 0000000..71f736f --- /dev/null +++ b/examples/countext2.ha @@ -0,0 +1,13 @@ +void main() { + short i,end; + @"Enter an limit\n"; + #end; + @"Start:\n"; + i=0; + while(i<=end){ + @i;@"\n"; + i++; + } + @"End\n"; + +} \ No newline at end of file diff --git a/examples/example.ha b/examples/example.ha index 7a36313..a64d27e 100644 --- a/examples/example.ha +++ b/examples/example.ha @@ -1,4 +1,4 @@ void main() { - @"Headache compiles to 8 bit Brainfuck\n"; + print "Headache compiles to 8 bit Brainfuck\n"; } \ No newline at end of file diff --git a/examples/experimental/sincos.ha b/examples/experimental/sincos.ha index df8cd45..4fb2a54 100644 --- a/examples/experimental/sincos.ha +++ b/examples/experimental/sincos.ha @@ -44,7 +44,7 @@ short cos(short x){ short i; short sum; - //sum = (x - x*x/2 + pow(x,4)/factorial(4))*100; + sum = (x - x*x/2 + pow(x,4)/factorial(4))*100; return sum; } void main() { diff --git a/examples/factor.ha b/examples/factor.ha new file mode 100644 index 0000000..8be3dda --- /dev/null +++ b/examples/factor.ha @@ -0,0 +1,28 @@ +byte modulus8(byte a, byte b){ + byte r,q; + q = a/b; + r = a - b*q; + return r; +} + +void factor(byte n) { + byte x,i; + print n; @"! = "; + x = n; + i = 2; + while (x > 1) { + if (modulus8(x,i) == 0) { + print " "; @i; + x = x / i; + i = 1; + } + i++; + } + print "\n"; + } +void main() { + byte n; + print "Enter number:\n"; + read n; + factor(n); +} \ No newline at end of file diff --git a/examples/factorial.ha b/examples/factorial.ha index ea94fe9..6d675da 100644 --- a/examples/factorial.ha +++ b/examples/factorial.ha @@ -8,11 +8,10 @@ } return z; } - void main(){ short x; @"Calculate x!\n"; @"Enter x:\n"; read x; - @"x! is "; @factorial(x); @"\n"; -} \ No newline at end of file + @x; @"! is "; @factorial(x); @"\n"; +} diff --git a/examples/masoque.ha b/examples/masoque.ha index daf1013..735c143 100644 --- a/examples/masoque.ha +++ b/examples/masoque.ha @@ -1,4 +1,4 @@ -void main() { +void hello() { print "Hello, World!\n"; } From 83206fefd14c2db2b7da805dd53e6e531b52114e Mon Sep 17 00:00:00 2001 From: LucasMW Date: Wed, 24 Aug 2022 21:56:45 +0100 Subject: [PATCH 19/20] as lib --- src/main.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 1fd2be1..b177138 100644 --- a/src/main.c +++ b/src/main.c @@ -56,6 +56,17 @@ int yy_lines=1; //save one for EOF extern FILE* yyin; +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif +extern YY_BUFFER_STATE yy_scan_string (char *base); + + char forceExpand=0; void lexError(const char* message, int ret) { @@ -88,11 +99,12 @@ static char optimizationOptionsCount = 3; static char* breakingOptions[] = { "--help", "--version", - "--maga" + "--maga", + "--xxx" }; static char breakingOptionsCount = 3; -static char hacVersion[] = "v0.71.3b (LLVM)"; +static char hacVersion[] = "v0.73.1b (LLVM)"; static int isOption(const char* candidate){ for (int i=0;i trump.jpg"); system("open trump.jpg"); + + const char* str = "void main() { byte a;\n#a;\nprint a;\n\n} "; + yy_scan_string((char*)str); + + + yyparse(); + + checkAndFixesTypesInTree(); + setOptimizationLevel(level); + optimizeTree(); + codeTree(); + return 0; - } + } } else { fileName = argv[1]; option = NULL; + yyin = fopen(fileName,"r"); } From f0e59cf9fb066c4abe362b6db4a6f41666c2b783 Mon Sep 17 00:00:00 2001 From: LucasMW Date: Wed, 24 Aug 2022 21:57:38 +0100 Subject: [PATCH 20/20] lib attempt and llvm *.o files --- Makefile | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1682240..7753145 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS = -Wall -std=c99 -O3 OUTFILE = hac SOURCES = src/main.c src/lex.c src/grammar.c src/tree.c src/lextest.c src/symbolTable.c src/codeGen.c src/testbfi.c src/compilerFunctions.c src/codeEss.c src/optimizer.c src/expander.c src/highlight.c src/codellvm.c -OBJS = temp/codeGen.o temp/symbolTable.o temp/grammar.o temp/tree.o temp/main.o temp/lex.o temp/lextest.o temp/testbfi.o temp/compilerFunctions.o temp/codeEss.o temp/optimizer.o temp/expander.o temp/highlight.o +OBJS = temp/codeGen.o temp/symbolTable.o temp/grammar.o temp/tree.o temp/main.o temp/lex.o temp/lextest.o temp/testbfi.o temp/compilerFunctions.o temp/codeEss.o temp/optimizer.o temp/expander.o temp/highlight.o temp/codellvm.o ZIGCC = zig cc WINCC = x86_64-w64-mingw32-gcc-11.1.0 #old one was i686-w64-mingw32-gcc; changed because false positives @@ -189,6 +189,18 @@ temp/lex.o: src/lex.c cc -o temp/lex.o -Wall -O3 -c src/lex.c temp/highlight.o: src/highlight.c cc -o temp/highlight.o -Wall -O3 -c src/highlight.c +temp/codellvm.o: src/codellvm.c + cc -o temp/codellvm.o -Wall -O3 -c src/codellvm.c + +## LIBS +hac.a: $(OBJS) + ar ruv hac.a temp/*.o + ranlib hac.a + +hac.so: $(OBJS) + cc -shared temp/*.o -o hac.so + + ## ZIG CC EXPERIMENT