Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
compiler-construction
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
User expired
compiler-construction
Commits
d9353a98
Commit
d9353a98
authored
5 years ago
by
FlorianKrull
Browse files
Options
Downloads
Patches
Plain Diff
Stack size and constants added
parent
8d81b229
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/mcc/assembly_gen.h
+27
-0
27 additions, 0 deletions
include/mcc/assembly_gen.h
resources/Modern Compiler Implementation in C.pdf
+0
-0
0 additions, 0 deletions
resources/Modern Compiler Implementation in C.pdf
src/assembly_gen.c
+67
-0
67 additions, 0 deletions
src/assembly_gen.c
with
94 additions
and
0 deletions
include/mcc/assembly_gen.h
+
27
−
0
View file @
d9353a98
#ifndef MCC_ASSEMBLY_GEN_H
#define MCC_ASSEMBLY_GEN_H
#include
"mcc/dynamic_array.h"
struct
mcc_assembly_data
{
int
array_index
;
int
parameter_counter
;
int
arg_counter
;
int
is_main
;
};
enum
mcc_asm_contant_type
{
MCC_ASM_CONST_FLOAT
,
MCC_ASM_CONST_STRING
};
struct
mcc_asm_constant
{
enum
mcc_asm_contant_type
const_type
;
int
number
;
struct
mcc_asm_constant
*
next
;
union
{
double
f_value
;
char
*
s_value
;
};
}
#endif //MCC_ASSEMBLY_GEN_H
This diff is collapsed.
Click to expand it.
resources/Modern Compiler Implementation in C.pdf
0 → 100644
+
0
−
0
View file @
d9353a98
File added
This diff is collapsed.
Click to expand it.
src/assembly_gen.c
+
67
−
0
View file @
d9353a98
...
...
@@ -4,10 +4,73 @@
#include
"mcc/tac.h"
#include
"mcc/tac_print.h"
#include
"mcc/symbol_table.h"
#include
"mcc/assembly_gen.h"
#define VARIABLE_SIZE 4
int
compute_stack
(
int
variable_size
)
{
int
stack_size
=
variable_size
*
VARIABLE_SIZE
;
if
(
stack_size
%
16
!=
0
)
{
stack_size
+=
(
16
-
stack_size
%
16
);
}
return
stack_size
;
}
void
mcc_asm_add_constant
(
struct
mcc_asm_constant
**
entries
,
enum
mcc_asm_constant_type
type
,
int
*
counter
,
void
*
value
){
struct
mcc_asm_constant
*
add_const
=
(
struct
mcc_asm_constant
*
)
malloc
(
sizeof
(
*
add_const
));
add_const
->
const_type
=
type
;
add_const
->
next
=
NULL
;
add_const
->
number
=
++
(
*
counter
);
if
(
type
==
MCC_ASM_CONST_STRING
){
add_const
->
s_value
=
(
char
*
)
value
;
}
else
if
(
type
==
MCC_ASM_CONST_FLOAT
){
add_const
->
f_value
=
*
((
double
*
)
value
);
}
if
(
*
entries
==
NULL
)
{
*
entries
=
add_const
;
}
else
{
struct
mcc_asm_constant
*
constant
=
*
entries
;
while
(
constant
->
next
!=
NULL
)
{
constant
=
constant
->
next
;
}
constant
->
next
=
add_const
;
}
}
void
mcc_asm_print_constant
(
FILE
*
out
,
struct
mcc_asm_constant
*
constant
)
{
if
(
constant
==
NULL
)
{
return
;
}
fprintf
(
out
,
"
\t
.section
\t
.rodata
\n
"
);
if
(
constant
==
NULL
)
{
struct
mcc_asm_constant
*
current
=
constant
;
while
(
current
!=
NULL
)
{
fprintf
(
out
,
".LC%d:
\n
"
,
current
->
number
);
switch
(
current
->
const_type
)
{
case
MCC_ASM_CONST_FLOAT
:
fprintf
(
out
,
"
\t
.float
\t
%lf
\n
"
,
current
->
f_value
);
break
;
case
MCC_ASM_CONST_STRING
:
fprintf
(
out
,
"
\t
.string
\t\"
%s
\"\n
"
,
current
->
s_value
);
break
;
default:
break
;
}
current
=
current
->
next
;
}
}
char
*
mcc_asm_gen
(
FILE
*
out
,
struct
mcc_tac
*
tac
)
{
int
const_count
=
0
;
struct
mcc_asm_constant
*
constants
=
NULL
;
for
(
int
i
=
0
;
i
<
tac
->
tac_entries
->
size
;
i
++
)
{
...
...
@@ -227,10 +290,12 @@ char *mcc_asm_gen(FILE *out, struct mcc_tac *tac)
fprintf
(
out
,
"
\t
movl
\t
$%s, %d(%%ebp)
\n
"
,
arg1
,
result
->
offset
);
break
;
case
MCC_TAC_STRING_LITERAL
:
mcc_asm_add_constant
(
&
constants
,
MCC_ASM_CONST_STRING
,
&
const_count
,
arg1
);
fprintf
(
out
,
"
\t
movl
\t
$.LCONST%d, %d(%%ebp)
\n
"
,
const_count
,
result
->
offset
);
break
;
case
MCC_TAC_FLOAT_LITERAL
:
//TODO create new constant
mcc_asm_add_constant
(
&
constants
,
MCC_ASM_CONST_FLOAT
,
&
const_count
,
arg1
);
fprintf
(
out
,
"
\t
flds
\t
.LCONST%d
\n
"
,
const_count
);
fprintf
(
out
,
"
\t
fstps
\t
%d(%%ebp)
\n
"
,
result
->
offset
);
break
;
...
...
@@ -339,6 +404,8 @@ char *mcc_asm_gen(FILE *out, struct mcc_tac *tac)
fprintf
(
out
,
"
\n
"
);
//TODO add constants
mcc_asm_print_constant
(
out
,
constants
);
//fprintf(out, ".LCONST%d:\n", i);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment