Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
2
2022fall-Compiler_CMinus
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李晓奇
2022fall-Compiler_CMinus
Commits
1de803c9
Commit
1de803c9
authored
Oct 27, 2022
by
李晓奇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
38 points
parent
ab890b55
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
12 deletions
+24
-12
Reports/3-ir-gen/report.md
Reports/3-ir-gen/report.md
+2
-0
include/cminusf_builder.hpp
include/cminusf_builder.hpp
+1
-1
src/cminusfc/cminusf_builder.cpp
src/cminusfc/cminusf_builder.cpp
+19
-11
src/cminusfc/cminusfc.cpp
src/cminusfc/cminusfc.cpp
+2
-0
No files found.
Reports/3-ir-gen/report.md
View file @
1de803c9
...
...
@@ -96,6 +96,8 @@ PB20111654 李晓奇
-
全局变量要特殊对待
-
数组下标为负时要调用
`neg_idx_except()`
-
神奇,bool变量不能比较,所以在比较、计算的时候要做类型转换
## 实验设计
...
...
include/cminusf_builder.hpp
View file @
1de803c9
...
...
@@ -96,7 +96,7 @@ class CminusfBuilder : public ASTVisitor {
virtual
void
visit
(
ASTTerm
&
)
override
final
;
virtual
void
visit
(
ASTCall
&
)
override
final
;
void
type_cast
(
Value
*&
,
Value
*&
,
std
::
string
=
"computation"
);
void
biop_type_check
(
Value
*&
,
Value
*&
,
std
::
string
=
"computation"
);
std
::
unique_ptr
<
IRBuilder
>
builder
;
Scope
scope
;
...
...
src/cminusfc/cminusf_builder.cpp
View file @
1de803c9
...
...
@@ -49,8 +49,19 @@ void error_exit(std::string s) {
// This function makes sure that
// 1. 2 values have same type
// 2. type is either i32 or float
void
CminusfBuilder
::
type_cast
(
Value
*&
lvalue
,
Value
*&
rvalue
,
std
::
string
util
)
{
assert
(
not
Type
::
is_eq_type
(
lvalue
->
get_type
(),
rvalue
->
get_type
()));
void
CminusfBuilder
::
biop_type_check
(
Value
*&
lvalue
,
Value
*&
rvalue
,
std
::
string
util
)
{
if
(
Type
::
is_eq_type
(
lvalue
->
get_type
(),
rvalue
->
get_type
()))
{
if
(
lvalue
->
get_type
()
->
is_integer_type
()
or
lvalue
->
get_type
()
->
is_float_type
())
{
// check for i1
if
(
Type
::
is_eq_type
(
lvalue
->
get_type
(),
INT1_T
))
{
lvalue
=
builder
->
create_zext
(
lvalue
,
INT32_T
);
rvalue
=
builder
->
create_zext
(
rvalue
,
INT32_T
);
}
}
else
error_exit
(
"not supported type cast for "
+
util
);
return
;
}
// only support cast between int and float: i32, i1, float
//
...
...
@@ -434,9 +445,8 @@ void CminusfBuilder::visit(ASTSimpleExpression &node) {
auto
lvalue
=
cur_value
;
node
.
additive_expression_r
->
accept
(
*
this
);
auto
rvalue
=
cur_value
;
// type cast
if
(
not
Type
::
is_eq_type
(
lvalue
->
get_type
(),
rvalue
->
get_type
()))
type_cast
(
lvalue
,
rvalue
,
"cmp"
);
// check type
biop_type_check
(
lvalue
,
rvalue
,
"cmp"
);
bool
float_cmp
=
lvalue
->
get_type
()
->
is_float_type
();
switch
(
node
.
op
)
{
case
OP_LE
:
{
...
...
@@ -496,9 +506,8 @@ void CminusfBuilder::visit(ASTAdditiveExpression &node) {
auto
lvalue
=
cur_value
;
node
.
term
->
accept
(
*
this
);
auto
rvalue
=
cur_value
;
// type cast
if
(
not
Type
::
is_eq_type
(
lvalue
->
get_type
(),
rvalue
->
get_type
()))
type_cast
(
lvalue
,
rvalue
,
"addop"
);
// check type
biop_type_check
(
lvalue
,
rvalue
,
"addop"
);
bool
float_type
=
lvalue
->
get_type
()
->
is_float_type
();
// now left and right is the same type
switch
(
node
.
op
)
{
...
...
@@ -530,9 +539,8 @@ void CminusfBuilder::visit(ASTTerm &node) {
auto
lvalue
=
cur_value
;
node
.
factor
->
accept
(
*
this
);
auto
rvalue
=
cur_value
;
// type cast
if
(
not
Type
::
is_eq_type
(
lvalue
->
get_type
(),
rvalue
->
get_type
()))
type_cast
(
lvalue
,
rvalue
,
"mul"
);
// check type
biop_type_check
(
lvalue
,
rvalue
,
"mul"
);
bool
float_type
=
lvalue
->
get_type
()
->
is_float_type
();
// now left and right is the same type
switch
(
node
.
op
)
{
...
...
src/cminusfc/cminusfc.cpp
View file @
1de803c9
...
...
@@ -4,6 +4,8 @@
#include <iostream>
#include <memory>
#include "logging.hpp"
using
namespace
std
::
literals
::
string_literals
;
void
print_help
(
std
::
string
exe_name
)
{
...
...
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