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
ed7cad81
Commit
ed7cad81
authored
5 years ago
by
krf41037
Browse files
Options
Downloads
Patches
Plain Diff
Create tac from expression
parent
da44e281
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/mcc/tac_build.h
+9
-0
9 additions, 0 deletions
include/mcc/tac_build.h
src/tac_build.c
+68
-6
68 additions, 6 deletions
src/tac_build.c
with
77 additions
and
6 deletions
include/tac_build.h
→
include/
mcc/
tac_build.h
+
9
−
0
View file @
ed7cad81
...
...
@@ -45,6 +45,9 @@ enum mcc_tac_operation{
};
// use quadruples like this : https://github.com/fauzanzaid/Quadruple-Three-Address-Code-in-C ??
struct
mcc_tac
{
enum
mcc_tac_operation
tac_op
;
...
...
@@ -57,6 +60,12 @@ struct mcc_tac{
};
struct
mcc_tac
*
mcc_create_tac
(
struct
mcc_ast_expression
*
expr
,
struct
mcc_tac
*
tac
);
struct
mcc_tac
*
mcc_new_tac
();
int
mcc_tac_add
(
struct
mcc_tac
*
tac_prev
,
struct
mcc_tac
*
tac_add
);
void
mcc_tac_delete
(
struct
mcc_tac
*
tac
);
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/tac_build.c
+
68
−
6
View file @
ed7cad81
#include
"mcc/tac_build.h"
struct
mcc_tac
*
mcc_new_tac
(
enum
mcc_tac_operation
op
,
struct
mcc_tac
*
prev
,
struct
mcc_tac
*
next
)
struct
mcc_tac
*
mcc_new_tac
()
{
struct
mcc_tac
*
tac
=
malloc
(
sizeof
(
tac
));
tac
->
tac_op
=
op
;
if
(
!
tac
)
{
return
NULL
;
}
tac
->
tac_op
=
NULL
;
tac
->
arg1
=
NULL
;
tac
->
arg2
=
NULL
;
tac
->
result
=
NULL
;
tac
->
prev
=
prev
;
tac
->
next
=
next
;
tac
->
prev
=
NULL
;
tac
->
next
=
NULL
;
return
tac
;
}
struct
mcc_tac
*
mcc_create_tac
(
struct
mcc_ast_expression
*
expr
,
struct
mcc_tac
*
tac
)
{
switch
(
expr
->
type
)
{
case
MCC_AST_EXPRESSION_TYPE_PARENTH
:
mcc_create_tac
(
expr
->
expression
,
tac
);
break
;
case
MCC_AST_EXPRESSION_TYPE_BINARY_OP
:
struct
mcc_tac
*
tac_bin
=
mcc_new_tac
();
if
(
expr
->
lhs
->
type
==
MCC_AST_EXPRESSION_TYPE_LITERAL
){
tac_bin
->
arg1
=
ast_literal_to_char
(
expr
->
lhs
,
tac
);
}
if
(
expr
->
rhs
->
type
==
MCC_AST_EXPRESSION_TYPE_LITERAL
){
// TODO literal to char
tac_bin
->
arg2
=
ast_literal_to_char
(
expr
->
rhs
,
tac
);
}
case
MCC_AST_EXPRESSION_TYPE_CALL_EXPRESSION
:
case
MCC_AST_EXPRESSION_TYPE_UNARY_OP
:
case
MCC_AST_EXPRESSION_TYPE_LITERAL
:
//TODO parse MCC_AST types to MCC_TAC_OP types
break
;
default:
break
;
}
}
char
*
ast_literal_to_char
(
struct
mcc_ast_expression
*
expr
,
struct
mcc_tac
*
tac
){
char
*
string
;
switch
(
expr
->
literal
->
type
)
{
case
MCC_AST_DATA_TYPE_INT
:
string
=
malloc
(
sizeof
(
char
)
*
(
myLog10
(
expr
->
literal
->
i_value
,
0
)
+
2
));
sprintf
(
string
,
"%ld"
,
expr
->
literal
->
i_value
);
break
;
case
MCC_AST_DATA_TYPE_FLOAT
:
string
=
malloc
(
sizeof
(
char
)
*
64
);
sprintf
(
string
,
"%lf"
,
expr
->
literal
->
f_value
);
break
;
case
MCC_AST_DATA_TYPE_STRING
:
string
=
expr
->
literal
->
s_value
;
break
;
case
MCC_AST_DATA_TYPE_BOOL
:
string
=
expr
->
literal
->
b_value
;
break
;
case
MCC_AST_DATA_TYPE_VOID
:
string
=
"void"
;
break
;
default:
string
=
"Error ast_literal_to_char"
;
}
return
string
;
}
\ 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