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
95cc7f06
Commit
95cc7f06
authored
5 years ago
by
Clemens Paumgarten
Browse files
Options
Downloads
Patches
Plain Diff
fix call expression in ast.c and finish tac-call-expression
parent
c51943b2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/ast.c
+3
-3
3 additions, 3 deletions
src/ast.c
src/tac_build.c
+9
-7
9 additions, 7 deletions
src/tac_build.c
src/tac_print.c
+22
-0
22 additions, 0 deletions
src/tac_print.c
test-files/test.mcc
+10
-8
10 additions, 8 deletions
test-files/test.mcc
with
44 additions
and
18 deletions
src/ast.c
+
3
−
3
View file @
95cc7f06
...
...
@@ -533,16 +533,16 @@ struct mcc_ast_argument *mcc_ast_new_argument(struct mcc_ast_expression *express
assert
(
expression
);
struct
mcc_ast_argument
*
argument
=
malloc
(
sizeof
(
*
argument
));
argument
->
expressions
=
mcc_create_dynamic_array
(
MCC_ARGUMENT_EXPRESSION_SIZE
);
mcc_add_to_array
(
argument
->
expressions
,
expression
);
return
argument
;
return
mcc_ast_add_new_argument
(
expression
,
argument
);
}
struct
mcc_ast_argument
*
mcc_ast_add_new_argument
(
struct
mcc_ast_expression
*
expression
,
struct
mcc_ast_argument
*
argument
)
{
assert
(
argument
);
assert
(
expression
);
if
(
mcc_add_to_array
(
argument
->
expressions
,
expression
)
==
1
)
{
perror
(
"Error adding expression argument"
);
}
...
...
This diff is collapsed.
Click to expand it.
src/tac_build.c
+
9
−
7
View file @
95cc7f06
...
...
@@ -61,6 +61,7 @@ void mcc_tac_parse_expression(struct mcc_ast_expression *expression, struct mcc_
case
MCC_AST_EXPRESSION_TYPE_CALL_EXPRESSION
:
{
// check if call expression has arguments and push/pop parameters
int
arg_size
=
0
;
bool
is_array
=
expression
->
bracket_expression
!=
NULL
;
if
(
expression
->
argument
!=
NULL
)
{
struct
mcc_ast_argument
*
argument
=
expression
->
argument
;
arg_size
=
argument
->
expressions
->
size
;
...
...
@@ -71,8 +72,6 @@ void mcc_tac_parse_expression(struct mcc_ast_expression *expression, struct mcc_
enum
mcc_ast_data_type
type
=
mcc_symbol_table_get_expression_return_type
(
parameter
,
tac
->
current_symbol_table
);
bool
is_array
=
expression
->
bracket_expression
!=
NULL
;
enum
mcc_tac_operation
op
=
mcc_convert_ast_type_to_tac_param_push
(
type
,
is_array
);
mcc_tac_parse_expression
(
parameter
,
tac
);
...
...
@@ -101,11 +100,10 @@ void mcc_tac_parse_expression(struct mcc_ast_expression *expression, struct mcc_
struct
mcc_ast_argument
*
argument
=
expression
->
argument
;
arg_size
=
argument
->
expressions
->
size
;
for
(
int
i
=
arg_size
;
i
>=
0
;
i
--
)
{
for
(
int
i
=
arg_size
-
1
;
i
>=
0
;
i
--
)
{
struct
mcc_ast_expression
*
parameter
=
argument
->
expressions
->
arr
[
i
];
enum
mcc_ast_data_type
type
=
mcc_symbol_table_get_expression_return_type
(
parameter
,
tac
->
current_symbol_table
);
bool
is_array
=
expression
->
bracket_expression
!=
NULL
;
enum
mcc_tac_operation
op
=
mcc_convert_ast_type_to_tac_param_pop
(
type
,
is_array
);
// get array size from symbol table
...
...
@@ -119,9 +117,13 @@ void mcc_tac_parse_expression(struct mcc_ast_expression *expression, struct mcc_
}
}
int
number_of_digits
=
mcc_get_number_of_digits
(
array_size
);
char
*
arg1
=
malloc
(
sizeof
(
char
)
*
number_of_digits
+
1
);
sprintf
(
arg1
,
"%d"
,
array_size
);
char
*
arg1
=
NULL
;
if
(
is_array
)
{
int
number_of_digits
=
mcc_get_number_of_digits
(
array_size
);
char
*
arg1
=
malloc
(
sizeof
(
char
)
*
number_of_digits
+
1
);
sprintf
(
arg1
,
"%d"
,
array_size
);
}
// char *arg1 = tac->last_temporary;
mcc_tac_create_and_add_new_entry
(
op
,
arg1
,
NULL
,
NULL
,
tac
);
...
...
This diff is collapsed.
Click to expand it.
src/tac_print.c
+
22
−
0
View file @
95cc7f06
...
...
@@ -18,6 +18,8 @@ static const char *tac_type(enum mcc_tac_operation type)
case
MCC_TAC_STORE_BOOL
:
case
MCC_TAC_EQ_BOOL
:
case
MCC_TAC_IDENTIFIER_BOOL
:
case
MCC_TAC_PARAM_BOOL
:
case
MCC_TAC_PARAM_POP_BOOL
:
case
MCC_TAC_NEQ_BOOL
:
return
"bool"
;
case
MCC_TAC_INT
:
case
MCC_TAC_INT_LITERAL
:
...
...
@@ -32,6 +34,8 @@ static const char *tac_type(enum mcc_tac_operation type)
case
MCC_TAC_LT
:
case
MCC_TAC_LTEQ
:
case
MCC_TAC_IDENTIFIER_INT
:
case
MCC_TAC_PARAM_INT
:
case
MCC_TAC_PARAM_POP_INT
:
case
MCC_TAC_GTEQ
:
return
"int"
;
case
MCC_TAC_FLOAT
:
case
MCC_TAC_IDENTIFIER_FLOAT
:
...
...
@@ -42,6 +46,7 @@ static const char *tac_type(enum mcc_tac_operation type)
case
MCC_TAC_MUL_FLOAT
:
case
MCC_TAC_DIV_FLOAT
:
case
MCC_TAC_PARAM_FLOAT
:
case
MCC_TAC_PARAM_POP_FLOAT
:
case
MCC_TAC_LOAD_FLOAT
:
case
MCC_TAC_STORE_FLOAT
:
case
MCC_TAC_LT_FLOAT
:
...
...
@@ -54,6 +59,7 @@ static const char *tac_type(enum mcc_tac_operation type)
case
MCC_TAC_IDENTIFIER_STRING
:
case
MCC_TAC_STRING_LITERAL
:
case
MCC_TAC_PARAM_STRING
:
case
MCC_TAC_PARAM_POP_STRING
:
case
MCC_TAC_LOAD_STRING
:
case
MCC_TAC_STORE_STRING
:
return
"string"
;
case
MCC_TAC_JMP
:
...
...
@@ -63,6 +69,15 @@ static const char *tac_type(enum mcc_tac_operation type)
case
MCC_TAC_RETURN
:
case
MCC_TAC_FUNCTION_END
:
case
MCC_TAC_UNKNOWN
:
return
"untyped"
;
case
MCC_TAC_PARAM_POP_BOOL_ARR
:
case
MCC_TAC_PARAM_BOOL_ARR
:
return
"bool_array"
;
case
MCC_TAC_PARAM_POP_INT_ARR
:
case
MCC_TAC_PARAM_INT_ARR
:
return
"int_array"
;
case
MCC_TAC_PARAM_POP_STRING_ARR
:
case
MCC_TAC_PARAM_STRING_ARR
:
return
"string_array"
;
case
MCC_TAC_PARAM_POP_FLOAT_ARR
:
case
MCC_TAC_PARAM_FLOAT_ARR
:
return
"float_arr"
;
}
return
"ERROR"
;
}
...
...
@@ -174,6 +189,13 @@ void tac_print(struct mcc_tac *tac, FILE *out)
fprintf
(
out
,
"param_push %s (%s)
\n
"
,
entry
->
arg1
,
op_type
);
break
;
case
MCC_TAC_PARAM_POP_BOOL
:
case
MCC_TAC_PARAM_POP_INT
:
case
MCC_TAC_PARAM_POP_FLOAT
:
case
MCC_TAC_PARAM_POP_STRING
:
fprintf
(
out
,
"param_pop %s (%s)
\n
"
,
entry
->
arg1
,
op_type
);
break
;
// load
case
MCC_TAC_LOAD_BOOL
:
case
MCC_TAC_LOAD_INT
:
...
...
This diff is collapsed.
Click to expand it.
test-files/test.mcc
+
10
−
8
View file @
95cc7f06
int a () {
int b;
b = 3;
return b;
}
int main() {
int a;
int b;
...
...
@@ -12,7 +5,9 @@ int main() {
int max;
a = (1 + 1);
b = a();
a = -a;
b = func_a(2);
if (a < b) {
max = b;
...
...
@@ -27,3 +22,10 @@ int main() {
return 0;
}
int func_a (int a) {
int b;
b = 3;
return b;
}
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