diff --git a/src/codegen.c b/src/codegen.c index a3c4dff..49748ce 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -493,6 +493,7 @@ int generateAssign(Instruction *inst) { cg = addCG(getResult(inst), offset); } + CGNode *op1CG = findCG(getTN(op1)); //add option for constant assignment (should be easy) if (isConst(op1) == true) { @@ -500,9 +501,8 @@ int generateAssign(Instruction *inst) { return 0; } - CGNode *op1CG = findCG(getTN(op1)); - if (op1CG == NULL) { - printdebug("generateAssign failed, op1 is not constant but not in CGlist"); + else if (op1CG == NULL) { + printdebug("generateAssign failed, %s is not constant but not in CGlist", getName(getTN(op1))); return -1; } @@ -608,35 +608,61 @@ int generateCall(Instruction *inst){ TNodeOrConst *op1 = getOperand1(inst); TNodeOrConst *op2 = getOperand2(inst); if (op1 == NULL) { - printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); + printdebug("%sgenerateFunctionCall failed, NULL operand", COLOR_RED); return -1; } if (op1 == NULL) { - printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED); + printdebug("%sgenerateFunctionCall failed, NULL operand", COLOR_RED); return -1; } if (getTN(op1) == NULL) { - printdebug("generateFunctionCall failed, NULL tablenode"); + printdebug("generateFunctionCall failed, NULL tablenode1"); return -1; } - if (getTN(op2) == NULL) { - printdebug("generateFunctionCall failed, NULL tablenode"); - return -1; - } + // if (getTN(op2) == NULL) { + // printdebug("generateFunctionCall failed, NULL tablenode2"); + // return -1; + //} fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1))); fprintf(cg_flag, "\taddq\t$%d, %%rsp\n", 8 + getConst(op1)); - + + //now for the return + CGNode *cg = findCG(getResult(inst)); + + if (cg == NULL) { + cg = addCG(getResult(inst), offset); + } + + fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#store return from call\n", getAddress(cg)); + return 0; } int generateReturn(Instruction *inst){ - return -1; - //will movl the result into the appropriate register and move the stack pointer/offset stuff back to correct value + TNodeOrConst *op1 = getOperand1(inst); + CGNode *cg; + + + if (op1 == NULL) { + printdebug("generateReturn failed, NULL operand"); + return -1; + } + + cg = findCG(getTN(op1)); + if (cg == NULL) { + printdebug("generateReturn failed, trying to return %s not in CGList", getName(getTN(op1))); + return -1; + } + + fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#return %s\n", getAddress(cg), getName(getTN(op1))); + fprintf(cg_flag, "\tpopq\t%%rbp\n"); + fprintf(cg_flag, "\tret\n"); + return 0; } int generateCopyRight(Instruction *inst){ return -1; @@ -658,7 +684,7 @@ int generateParam(Instruction *inst){ CGNode *op1CG = findCG(getTN(op1)); if (op1CG == NULL) { - printdebug("generateParam failed, op1 is not in CGlist"); + printdebug("generateParam failed, %s is not in CGlist", getName(getTN(op1))); return -1; } @@ -668,7 +694,7 @@ int generateParam(Instruction *inst){ } int generateFunctionStart(Instruction *inst) { - + currentsp = offset; TableNode *funDecTN = getResult(inst); if (funDecTN == NULL) { @@ -690,20 +716,18 @@ int generateFunctionStart(Instruction *inst) { TableNode *paramTN = getParameter(getTypeEntry(funDecTN)); SymbolTable *st = getFunScope(funDecTN); int paramOffset = 16; + TableNode *tnToAdd = getFirstEntry(st); if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) { - fprintf(stderr, "%d\n", getAdInfoType(funDecTN)); - CGNode *paramCG = addCG(paramTN, currentsp); + CGNode *paramCG = addCG(tnToAdd, offset); fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset); - fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getPrimSize(getTypeEntry(paramTN))); + fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG)); } else { - fprintf(stderr, "record type\n"); int numParams = getRecLength(paramTN); - TableNode *tnToAdd = getFirstEntry(st); for (int i = 0; i < numParams; i++) { - CGNode *paramCG = addCG(tnToAdd, currentsp); + CGNode *paramCG = addCG(tnToAdd, offset); fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset); - fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getPrimSize(getTypeEntry(paramTN))); - paramOffset += getPrimSize(getTypeEntry(tnToAdd)); + fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG)); + paramOffset = getPrimSize(getTypeEntry(tnToAdd)); tnToAdd = getNextEntry(tnToAdd); } } diff --git a/src/grammar.y b/src/grammar.y index 9d5d5a3..fb33253 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -1244,7 +1244,7 @@ constant: char* temp = temp_var_gen(); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, chara, temp, NULL); emit_assignment(node, tn_or_const(CHARACTER,&$1)); - printdebug("string of C_CHARACTER in constant is %s",$1); + //printdebug("string of C_CHARACTER in constant is %s",$1); $$ = node; } diff --git a/tests/carl/NoErrors/functionValue.alpha b/tests/carl/NoErrors/functionValue.alpha index c1134ce..cac92bb 100644 --- a/tests/carl/NoErrors/functionValue.alpha +++ b/tests/carl/NoErrors/functionValue.alpha @@ -28,8 +28,8 @@ function c: string2int_2_integer function d: iic2b d(x,y,z) := { - [string: s;integer: s] - return (x < y & z < 'm'); + [string: s] + return 0; } function entry: string2int @@ -61,7 +61,7 @@ entry(arg) := { temp := a("Hello"); f := b(temp); result := c(f); - if (d(1,2,char)) + if (d(1,2,'a')) then { result := 0; }