Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Akshay Yeluri
project02-Akshay
Commits
56e0cd8a
Commit
56e0cd8a
authored
6 years ago
by
Akshay Yeluri
Browse files
Options
Download
Email Patches
Plain Diff
Implemented stage 7, added the prime-generating program as a test
parent
aafe6666
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/compile.c
+30
-2
src/compile.c
with
30 additions
and
2 deletions
+30
-2
src/compile.c
View file @
56e0cd8a
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include "compile.h"
#include "compile.h"
static
int
nextLabel
=
0
;
/**
/**
* Compiles the provided AST printing the resulting ASM to
* Compiles the provided AST printing the resulting ASM to
...
@@ -48,6 +49,11 @@ bool compile_ast(node_t *node) {
...
@@ -48,6 +49,11 @@ bool compile_ast(node_t *node) {
printf
(
" cqto
\n
"
);
printf
(
" cqto
\n
"
);
printf
(
" idivq %%rcx
\n
"
);
printf
(
" idivq %%rcx
\n
"
);
return
true
;
return
true
;
case
'>'
:
case
'<'
:
case
'='
:
printf
(
" cmp %%rax, %%rcx
\n
"
);
return
true
;
default:
default:
return
false
;
return
false
;
}
}
...
@@ -67,12 +73,34 @@ bool compile_ast(node_t *node) {
...
@@ -67,12 +73,34 @@ bool compile_ast(node_t *node) {
}
}
else
if
(
node
->
type
==
LABEL
)
{
else
if
(
node
->
type
==
LABEL
)
{
label_node_t
*
p
=
(
label_node_t
*
)
node
;
label_node_t
*
p
=
(
label_node_t
*
)
node
;
printf
(
"%s:
\n
"
,
p
->
label
);
printf
(
"
L
%s:
\n
"
,
p
->
label
);
return
true
;
return
true
;
}
}
else
if
(
node
->
type
==
GOTO
)
{
else
if
(
node
->
type
==
GOTO
)
{
goto_node_t
*
p
=
(
goto_node_t
*
)
node
;
goto_node_t
*
p
=
(
goto_node_t
*
)
node
;
printf
(
"jmp %s
\n
"
,
p
->
label
);
printf
(
" jmp L%s
\n
"
,
p
->
label
);
return
true
;
}
else
if
(
node
->
type
==
COND
)
{
cond_node_t
*
cond
=
(
cond_node_t
*
)
node
;
if
(
!
compile_ast
(
cond
->
cond
))
{
return
false
;
}
binary_node_t
*
bin
=
(
binary_node_t
*
)
cond
->
cond
;
char
op
=
bin
->
op
;
switch
(
op
)
{
case
'>'
:
printf
(
" jng L%d
\n
"
,
nextLabel
);
break
;
case
'<'
:
printf
(
" jnl L%d
\n
"
,
nextLabel
);
break
;
case
'='
:
printf
(
" jne L%d
\n
"
,
nextLabel
);
break
;
default:
return
false
;
}
if
(
!
compile_ast
(
cond
->
if_branch
))
{
return
false
;
}
printf
(
"L%d:
\n
"
,
nextLabel
++
);
return
true
;
return
true
;
}
}
return
false
;
return
false
;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment