Compare commits
3 Commits
2a3c76cb6e
...
75c8e5dc68
Author | SHA1 | Date | |
---|---|---|---|
75c8e5dc68 | |||
77376dc1ef | |||
57cc3f62fc |
28
exercises/06/.vscode/launch.json
vendored
Normal file
28
exercises/06/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
// for Linux
|
||||||
|
"name": "(gdb) Launch euclid codegen 2",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceRoot}/vslc/src/vslc",
|
||||||
|
"args": ["<", "${workspaceFolder}/vslc/vsl_programs/ps6-codegen2/euclid.vsl"],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": true,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -16,7 +16,8 @@
|
|||||||
static uint64_t
|
static uint64_t
|
||||||
stack_depth,
|
stack_depth,
|
||||||
if_seq,
|
if_seq,
|
||||||
while_seq;
|
while_seq,
|
||||||
|
closest_while;
|
||||||
|
|
||||||
/**Generate table of strings in a rodata section. */
|
/**Generate table of strings in a rodata section. */
|
||||||
void generate_stringtable ( void );
|
void generate_stringtable ( void );
|
||||||
@ -251,9 +252,9 @@ generate_node ( node_t *node)
|
|||||||
switch (node->type)
|
switch (node->type)
|
||||||
{
|
{
|
||||||
case ASSIGNMENT_STATEMENT:
|
case ASSIGNMENT_STATEMENT:
|
||||||
|
// First solve the rhs
|
||||||
solve_expressions(node->children[1]);
|
solve_expressions(node->children[1]);
|
||||||
//ASM(popq, %rax);
|
// Then store in lhs
|
||||||
//POP(%rax);
|
|
||||||
writeback_variable(node->children[0], "%rax");
|
writeback_variable(node->children[0], "%rax");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -309,10 +310,10 @@ generate_node ( node_t *node)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case WHILE_STATEMENT:
|
case WHILE_STATEMENT:
|
||||||
/* DO NOTHING YET */
|
generate_while_statement(node);
|
||||||
break;
|
break;
|
||||||
case NULL_STATEMENT:
|
case NULL_STATEMENT:
|
||||||
/* USED IN WHILE */
|
solve_continue_statement();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
@ -511,13 +512,25 @@ generate_function_call(node_t *node)
|
|||||||
|
|
||||||
for (int arg = 0; arg < MIN(NO_REG_RECORD, arg_list->n_children); arg++)
|
for (int arg = 0; arg < MIN(NO_REG_RECORD, arg_list->n_children); arg++)
|
||||||
{
|
{
|
||||||
if (arg_list->children[arg]->type == NUMBER_DATA)
|
switch (arg_list->children[arg]->type)
|
||||||
|
{
|
||||||
|
case NUMBER_DATA:
|
||||||
printf("\tmovq\t$%ld, %s\n",
|
printf("\tmovq\t$%ld, %s\n",
|
||||||
*(int64_t*)arg_list->children[arg]->data,
|
*(int64_t*)arg_list->children[arg]->data,
|
||||||
record[arg]
|
record[arg]
|
||||||
);
|
);
|
||||||
else
|
break;
|
||||||
|
|
||||||
|
case EXPRESSION:
|
||||||
|
solve_expressions(arg_list->children[arg]);
|
||||||
|
printf("\tmovq\t%%rax, %s\n",record[arg]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
fetch_variable(arg_list->children[arg], record[arg]);
|
fetch_variable(arg_list->children[arg], record[arg]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arg_list->n_children > NO_REG_RECORD)
|
if (arg_list->n_children > NO_REG_RECORD)
|
||||||
@ -528,19 +541,30 @@ generate_function_call(node_t *node)
|
|||||||
|
|
||||||
for (int arg = arg_list->n_children - 1; arg >= NO_REG_RECORD; arg--)
|
for (int arg = arg_list->n_children - 1; arg >= NO_REG_RECORD; arg--)
|
||||||
{
|
{
|
||||||
if (arg_list->children[arg]->type == NUMBER_DATA)
|
switch (arg_list->children[arg]->type)
|
||||||
|
{
|
||||||
|
case NUMBER_DATA:
|
||||||
printf("\tpushq\t$%ld\t\t\t\t# PUSH: %ld\n",
|
printf("\tpushq\t$%ld\t\t\t\t# PUSH: %ld\n",
|
||||||
*(int64_t*)arg_list->children[arg]->data,
|
*(int64_t*)arg_list->children[arg]->data,
|
||||||
++stack_depth
|
++stack_depth
|
||||||
);
|
);
|
||||||
else
|
break;
|
||||||
{
|
|
||||||
|
// Arg can be an expression
|
||||||
|
case EXPRESSION:
|
||||||
|
solve_expressions(arg_list->children[arg]);
|
||||||
|
PUSH(%rax);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
printf("\tpushq\t");
|
printf("\tpushq\t");
|
||||||
generate_var_ident(arg_list->children[arg]);
|
generate_var_ident(arg_list->children[arg]);
|
||||||
printf("\t\t\t\t# PUSH: %ld", ++stack_depth);
|
printf("\t\t\t\t# PUSH: %ld", ++stack_depth);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
|
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
|
||||||
@ -548,6 +572,7 @@ generate_function_call(node_t *node)
|
|||||||
// Aaaand pop the stack to return back to stack alignment
|
// Aaaand pop the stack to return back to stack alignment
|
||||||
if (isStack16ByteAligned)
|
if (isStack16ByteAligned)
|
||||||
POP(%rcx);
|
POP(%rcx);
|
||||||
|
|
||||||
printf("# End of function call\n");
|
printf("# End of function call\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -571,15 +596,10 @@ solve_statements(node_t *node, char *operator)
|
|||||||
writeback_variable(node->children[0], "%rax");
|
writeback_variable(node->children[0], "%rax");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Takes in a relation/number node and sets %rax to true if the statement is true
|
||||||
void
|
void
|
||||||
generate_if_statement(node_t *node)
|
solve_relations(node_t *relation_root)
|
||||||
{
|
{
|
||||||
uint64_t current_if_seq = if_seq++;
|
|
||||||
|
|
||||||
COMMENT("Begin IF %ld", current_if_seq);
|
|
||||||
|
|
||||||
node_t *relation_root = node->children[0];
|
|
||||||
|
|
||||||
switch (relation_root->type)
|
switch (relation_root->type)
|
||||||
{
|
{
|
||||||
case NUMBER_DATA:
|
case NUMBER_DATA:
|
||||||
@ -603,47 +623,80 @@ generate_if_statement(node_t *node)
|
|||||||
POP(%rax);
|
POP(%rax);
|
||||||
|
|
||||||
ASM(cmp, %r10, %rax);
|
ASM(cmp, %r10, %rax);
|
||||||
|
ASM(movq, $0, %rax);
|
||||||
|
|
||||||
switch (*(char*)relation_root->data)
|
switch (*(char*)relation_root->data)
|
||||||
{
|
{
|
||||||
case '=':
|
case '=':
|
||||||
ASM(sete, %rax);
|
ASM(sete, %al);
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
ASM(setg, %rax);
|
ASM(setg, %al);
|
||||||
break;
|
break;
|
||||||
case '<':
|
case '<':
|
||||||
ASM(setl, %rax);
|
ASM(setl, %al);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
generate_if_statement(node_t *node)
|
||||||
|
{
|
||||||
|
uint64_t current_if_seq = if_seq++;
|
||||||
|
|
||||||
|
COMMENT("Begin IF %ld", current_if_seq);
|
||||||
|
|
||||||
|
// The realtion is allways in the first part of the IF
|
||||||
|
solve_relations(node->children[0]);
|
||||||
|
|
||||||
ASM(cmp, $1, %rax);
|
ASM(cmp, $1, %rax);
|
||||||
printf("\tjne\t%s%03ld\n", (node->n_children > 2) ? "ELSE" : "ENDIF", current_if_seq);
|
printf("\tjne\t%s%03ld\n", (node->n_children > 2) ? "ELSE" : "ENDIF", current_if_seq);
|
||||||
solve_expressions(node->children[1]);
|
generate_node(node->children[1]);
|
||||||
|
|
||||||
if (node->n_children > 2) {
|
if (node->n_children > 2) {
|
||||||
printf("\tjmp\tENDIF%03ld\n", current_if_seq);
|
printf("\tjmp \tENDIF%03ld\n", current_if_seq);
|
||||||
printf("ELSE%03ld:\n", current_if_seq);
|
printf("ELSE%03ld:\n", current_if_seq);
|
||||||
solve_expressions(node->children[2]);
|
|
||||||
|
generate_node(node->children[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("ENDIF%03ld:\n", current_if_seq);
|
|
||||||
|
|
||||||
COMMENT("End IF %ld", current_if_seq);
|
COMMENT("End IF %ld", current_if_seq);
|
||||||
|
printf("ENDIF%03ld:\n", current_if_seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
generate_while_statement(node_t *node)
|
generate_while_statement(node_t *node)
|
||||||
{
|
{
|
||||||
|
uint64_t current_while_seq = while_seq++;
|
||||||
|
|
||||||
|
uint64_t prev_closest_while = closest_while;
|
||||||
|
closest_while = current_while_seq;
|
||||||
|
|
||||||
|
COMMENT("Begin WHILE %ld", current_while_seq);
|
||||||
|
printf("WHILE%03ld:\n", current_while_seq);
|
||||||
|
|
||||||
|
// Relation is allways the first entry in a while
|
||||||
|
solve_relations(node->children[0]);
|
||||||
|
|
||||||
|
ASM(cmp, $1, %rax);
|
||||||
|
|
||||||
|
printf("\tjne\tENDWHILE%03ld\n", current_while_seq);
|
||||||
|
|
||||||
|
generate_node(node->children[1]);
|
||||||
|
closest_while = prev_closest_while;
|
||||||
|
|
||||||
|
printf("\tjmp \tWHILE%03ld\n", current_while_seq);
|
||||||
|
printf("ENDWHILE%03ld:\n", current_while_seq);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
solve_continue_statement()
|
solve_continue_statement()
|
||||||
{
|
{
|
||||||
|
COMMENT("Continue to WHILE%03ld", closest_while);
|
||||||
|
printf("\tjmp \tWHILE%03ld\n", closest_while);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Generates the main function with argument parsing and calling of our
|
/**Generates the main function with argument parsing and calling of our
|
||||||
|
@ -28,4 +28,6 @@ func main() begin
|
|||||||
if my_func(1, 2, 3, 5, 8, 13, 21, 34) > 3 then begin
|
if my_func(1, 2, 3, 5, 8, 13, 21, 34) > 3 then begin
|
||||||
print "True!"
|
print "True!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return 0
|
||||||
end
|
end
|
57
exercises/06/vslc/vsl_programs/ps3-simplify/while_test.vsl
Normal file
57
exercises/06/vslc/vsl_programs/ps3-simplify/while_test.vsl
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// This program is a simple test of while loops, counting down from 19 to 0
|
||||||
|
// and skipping 10 (if continue is implemented)
|
||||||
|
|
||||||
|
func while_test ()
|
||||||
|
begin
|
||||||
|
var a, b
|
||||||
|
a := 20
|
||||||
|
|
||||||
|
b := test_while()
|
||||||
|
|
||||||
|
print a
|
||||||
|
|
||||||
|
|
||||||
|
if a > 0 then print "foobar"
|
||||||
|
while a > 0 do
|
||||||
|
begin
|
||||||
|
if a = 10 then
|
||||||
|
begin
|
||||||
|
a -= 1
|
||||||
|
print "Skip..."
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
else
|
||||||
|
a -= 1
|
||||||
|
print a
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
func test_while()
|
||||||
|
begin
|
||||||
|
var n, m
|
||||||
|
|
||||||
|
n := 4
|
||||||
|
m := 21
|
||||||
|
|
||||||
|
while n > 0 do
|
||||||
|
begin
|
||||||
|
n -= 1
|
||||||
|
if n = 2 then
|
||||||
|
continue
|
||||||
|
|
||||||
|
while m > 0 do
|
||||||
|
begin
|
||||||
|
m -= 1
|
||||||
|
|
||||||
|
if m = 10 then
|
||||||
|
continue
|
||||||
|
|
||||||
|
print n, m
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
return 0
|
||||||
|
end
|
35
exercises/06/vslc/vsl_programs/ps6-codegen2/lists.vsl
Normal file
35
exercises/06/vslc/vsl_programs/ps6-codegen2/lists.vsl
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
func my_func(a, b, c, d, e, f, g, h) begin
|
||||||
|
var i, j, k, l, m
|
||||||
|
|
||||||
|
i := a + b + d
|
||||||
|
|
||||||
|
print i, f
|
||||||
|
|
||||||
|
if i = f then begin
|
||||||
|
print "hmmm"
|
||||||
|
end
|
||||||
|
|
||||||
|
if 1 < 2 then begin
|
||||||
|
print "Whaa"
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
print "Whooo"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
|
||||||
|
func main() begin
|
||||||
|
var n, o, p, q, r, s, t, u, v, w
|
||||||
|
n := 5
|
||||||
|
n += my_func(1, 2, 3, 5, 8, 13, 21, 34)
|
||||||
|
|
||||||
|
if my_func(1, 2, 3, 5, 8, 13, 21, 34) > 3 then begin
|
||||||
|
print "True!"
|
||||||
|
end
|
||||||
|
|
||||||
|
return 0
|
||||||
|
end
|
@ -3,9 +3,14 @@
|
|||||||
|
|
||||||
func while_test ()
|
func while_test ()
|
||||||
begin
|
begin
|
||||||
var a
|
var a, b
|
||||||
a := 20
|
a := 20
|
||||||
|
|
||||||
|
b := test_while()
|
||||||
|
|
||||||
print a
|
print a
|
||||||
|
|
||||||
|
|
||||||
if a > 0 then print "foobar"
|
if a > 0 then print "foobar"
|
||||||
while a > 0 do
|
while a > 0 do
|
||||||
begin
|
begin
|
||||||
@ -21,3 +26,32 @@ begin
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
func test_while()
|
||||||
|
begin
|
||||||
|
var n, m
|
||||||
|
|
||||||
|
n := 4
|
||||||
|
m := 21
|
||||||
|
|
||||||
|
while n > 0 do
|
||||||
|
begin
|
||||||
|
n -= 1
|
||||||
|
if n = 2 then
|
||||||
|
continue
|
||||||
|
|
||||||
|
while m > 0 do
|
||||||
|
begin
|
||||||
|
m -= 1
|
||||||
|
|
||||||
|
if m = 10 then
|
||||||
|
continue
|
||||||
|
|
||||||
|
print n, m
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
return 0
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user