Skip to content

Commit

Permalink
fixed parameter bug
Browse files Browse the repository at this point in the history
  • Loading branch information
songmeric committed Mar 22, 2024
1 parent 67c99d2 commit 28ef5d0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 30 deletions.
4 changes: 4 additions & 0 deletions src/ast_compound_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

void CompoundStatement::EmitRISC(std::ostream &stream, Context &context) const
{
context.EnterScope();

// Emit assembler directives.
if (decls_)
decls_->EmitRISC(stream, context);
if (stmts_)
stmts_->EmitRISC(stream, context);

context.ExitScope();
}

void CompoundStatement::Print(std::ostream &stream) const
Expand Down
18 changes: 9 additions & 9 deletions src/ast_function_definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ void FunctionDefinition::EmitRISC(std::ostream &stream, Context &context) const
stream << "mv s0,sp\n";

// Create the parameter variables
Node *paramList = context.functionParameters;
if (paramList)
paramList->EmitRISC(stream, context);
// Node *paramList = context.functionParameters;
// if (paramList)
// paramList->EmitRISC(stream, context);

int parameter_nr = 0;

Expand All @@ -57,13 +57,13 @@ void FunctionDefinition::EmitRISC(std::ostream &stream, Context &context) const
}

if (!parameterInstances.empty()) {
Variable *firstParameter = parameterInstances.front();
// Variable *firstParameter = parameterInstances.front();
Variable *lastParameter = parameterInstances.back();
size_t end = lastParameter->offset +
context.SizeOfType(lastParameter->type);
size_t start = firstParameter->offset;
ssize_t alloc = end - start;

// size_t end = lastParameter->offset +
// context.SizeOfType(lastParameter->type);
// size_t start = firstParameter->offset;
// ssize_t alloc = end - start;
ssize_t alloc = lastParameter->offset;
stream << "# Allocate space for the parameters\n";
stream << "addi sp,sp," << alloc << "\n";
}
Expand Down
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int f() {
int f(int a, int b, int c, int d, int e, int f, int g, int h) {
int x = 5;
int y = 3;
return y;
Expand Down
20 changes: 0 additions & 20 deletions test.s
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
.text
.globl f
f:
# Allocate space to save s0
addi sp,sp,-4
# Save caller's s0
sw s0,0(sp)
# Copy stack pointer into frame pointer
mv s0,sp
# Load int constant
li a0, 5
addi sp,sp,-4
sw a0,-4(s0)
# Load int constant
li a0, 3
addi sp,sp,-4
sw a0,-8(s0)
# Load variable "y"
lw a0,-8(s0)
mv sp,s0
lw s0,(sp)
addi sp,sp,4
ret

0 comments on commit 28ef5d0

Please sign in to comment.