ps6: task 4
This commit is contained in:
@@ -240,12 +240,37 @@ static const char* generate_array_access(node_t* node)
|
||||
return MEM(RCX);
|
||||
}
|
||||
|
||||
int ternary_counter = 0;
|
||||
|
||||
// Generates code to evaluate expressions on the form A ? B : C
|
||||
// If A is non-zero, B is evaluated. Otherwise C is evaluated.
|
||||
// The final result is placed in %rax
|
||||
static void generate_ternary_operator(node_t* op)
|
||||
{
|
||||
// TODO (Task 4): Implement the ternary operator
|
||||
assert(op->n_children == 3);
|
||||
|
||||
node_t *a, *b, *c;
|
||||
a = op->children[0];
|
||||
b = op->children[1];
|
||||
c = op->children[2];
|
||||
|
||||
char endif_label[8];
|
||||
snprintf(endif_label, 8, "ENDTY%d", ternary_counter);
|
||||
char else_label[8];
|
||||
snprintf(else_label, 8, "ELSY%d", ternary_counter);
|
||||
|
||||
ternary_counter++;
|
||||
|
||||
generate_expression(a);
|
||||
CMPQ("$0", RAX);
|
||||
JE(else_label);
|
||||
generate_expression(b);
|
||||
JMP(endif_label);
|
||||
|
||||
LABEL("%s", else_label);
|
||||
generate_expression(c);
|
||||
LABEL("%s", endif_label);
|
||||
}
|
||||
|
||||
// Generates code to evaluate the expression, and place the result in %rax
|
||||
|
||||
Reference in New Issue
Block a user