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
6d308da4
Commit
6d308da4
authored
Oct 27, 2022
by
李晓奇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
65 points
parent
d2f3c996
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
27 deletions
+39
-27
Reports/3-ir-gen/report.md
Reports/3-ir-gen/report.md
+11
-5
src/cminusfc/cminusf_builder.cpp
src/cminusfc/cminusf_builder.cpp
+19
-18
src/cminusfc/cminusfc.cpp
src/cminusfc/cminusfc.cpp
+8
-3
tests/3-ir-gen/testcases/lv1/assign_int_array_local.cminus
tests/3-ir-gen/testcases/lv1/assign_int_array_local.cminus
+1
-1
No files found.
Reports/3-ir-gen/report.md
View file @
6d308da4
...
...
@@ -79,11 +79,7 @@ PB20111654 李晓奇
换句话说,访问
`ASTCall`
时,需要依次调用args的
`accept()`
,但是这个args!!!!expression应该返回的就是值才对。问题出现在变量的访问那里。
这个在访问
`ASTVar`
时解决掉:
-
如果做左值,返回指针元素
-
如果做右值,返回值
这个在访问
`ASTVar`
时解决掉:加入全局变量
`LV`
表式当前处理的是左值,所以要返回对应的地址,否则返回load出来的右值。
```
cpp
struct
ASTCall
:
ASTFactor
{
...
...
@@ -100,6 +96,16 @@ PB20111654 李晓奇
-
神奇,bool变量不能比较,所以在比较、计算的时候要做类型转换
-
store的顺序颠倒了!!
-
GEP和Load的翻译有问题。
定位问题:源操作数不是array类型!
那一定要看声明时push进scope的是什么,发现声明的数组类型其实也是指针类型(指向数组的指针),并不是数组??
但后来发现,原来问题出在调用GEP,我直接吧0传入了,“胡闹吗这不是”。
-
不要乱给lable命名,因为lable的命名不会自动(针对自定义)去重。
## 实验设计
...
...
src/cminusfc/cminusf_builder.cpp
View file @
6d308da4
...
...
@@ -155,6 +155,7 @@ void CminusfBuilder::visit(ASTVarDeclaration &node) {
default:
error_exit
(
"Variable type(not array) is not int or float"
);
}
assert
(
cur_value
->
get_type
()
->
is_pointer_type
()
&&
"IF SEE THIS: API ERROR"
);
}
else
{
// flat int or float type
...
...
@@ -302,12 +303,14 @@ void CminusfBuilder::visit(ASTSelectionStmt &node) {
scope
.
enter
();
node
.
expression
->
accept
(
*
this
);
auto
cond
=
cur_value
;
auto
TBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
"trueCase"
,
cur_fun
);
auto
FBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
"falseCase"
,
cur_fun
);
builder
->
create_cond_br
(
cond
,
TBB
,
FBB
);
auto
TBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
""
,
cur_fun
);
auto
passBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
""
,
cur_fun
);
builder
->
create_cond_br
(
cond
,
TBB
,
passBB
);
builder
->
set_insert_point
(
TBB
);
node
.
if_statement
->
accept
(
*
this
);
builder
->
set_insert_point
(
FBB
);
builder
->
set_insert_point
(
passBB
);
if
(
node
.
else_statement
)
node
.
else_statement
->
accept
(
*
this
);
scope
.
exit
();
...
...
@@ -318,9 +321,9 @@ void CminusfBuilder::visit(ASTIterationStmt &node) {
//!TODO: This function is empty now.
// Add some code here.
scope
.
enter
();
auto
HEAD
=
BasicBlock
::
create
(
builder
->
get_module
(),
"
loop_head
"
,
cur_fun
);
auto
BODY
=
BasicBlock
::
create
(
builder
->
get_module
(),
"
loop_body
"
,
cur_fun
);
auto
END
=
BasicBlock
::
create
(
builder
->
get_module
(),
"
end
"
,
cur_fun
);
auto
HEAD
=
BasicBlock
::
create
(
builder
->
get_module
(),
""
,
cur_fun
);
auto
BODY
=
BasicBlock
::
create
(
builder
->
get_module
(),
""
,
cur_fun
);
auto
END
=
BasicBlock
::
create
(
builder
->
get_module
(),
""
,
cur_fun
);
builder
->
create_br
(
HEAD
);
...
...
@@ -374,12 +377,15 @@ void CminusfBuilder::visit(ASTVar &node) {
// 1. get base
// 2. get bias if there is, and cast it if needed.
bool
old_LV
=
LV
;
auto
memory
=
scope
.
find
(
node
.
id
);
Value
*
addr
;
if
(
memory
==
nullptr
)
error_exit
(
"variable "
+
node
.
id
+
" not declared"
);
LOG_DEBUG
<<
"find entry: "
<<
node
.
id
<<
" "
<<
memory
;
if
(
node
.
expression
)
{
// e.g. int a[10]; // mem is [i32 x 10]*
assert
(
memory
->
get_type
()
->
is_pointer_type
());
LV
=
false
;
node
.
expression
->
accept
(
*
this
);
if
(
not
Type
::
is_eq_type
(
cur_value
->
get_type
(),
INT32_T
))
{
if
(
Type
::
is_eq_type
(
cur_value
->
get_type
(),
FLOAT_T
))
...
...
@@ -390,20 +396,22 @@ void CminusfBuilder::visit(ASTVar &node) {
auto
cond
=
builder
->
create_icmp_lt
(
cur_value
,
CONST_INT
(
0
));
auto
except_func
=
scope
.
find
(
"neg_idx_except"
);
auto
TBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
"
BadIdx
"
,
cur_fun
);
auto
FBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
"
Pass
"
,
cur_fun
);
auto
TBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
""
,
cur_fun
);
auto
FBB
=
BasicBlock
::
create
(
builder
->
get_module
(),
""
,
cur_fun
);
builder
->
create_cond_br
(
cond
,
TBB
,
FBB
);
builder
->
set_insert_point
(
TBB
);
builder
->
create_call
(
except_func
,
{});
builder
->
create_br
(
FBB
);
builder
->
set_insert_point
(
FBB
);
addr
=
builder
->
create_gep
(
memory
,
{
0
,
cur_value
});
addr
=
builder
->
create_gep
(
memory
,
{
CONST_INT
(
0
)
,
cur_value
});
}
else
{
// e.g. int a; // a is i32*
addr
=
memory
;
}
LV
=
old_LV
;
if
(
LV
)
cur_value
=
addr
;
else
{
...
...
@@ -583,13 +591,6 @@ void CminusfBuilder::visit(ASTCall &node) {
Type
*
param_type
=
func
->
get_function_type
()
->
get_param_type
(
i
);
node
.
args
[
i
]
->
accept
(
*
this
);
/* if (dynamic_cast<Constant *>(cur_value) != nullptr) {
* // this is literal
* cur_value = dynamic_cast<Constant *>(cur_value);
* } else {
* // this is variable
* cur_value = builder->create_load(cur_value);
* } */
// type cast
if
(
not
Type
::
is_eq_type
(
param_type
,
cur_value
->
get_type
()))
{
if
(
param_type
->
is_pointer_type
())
{
...
...
@@ -597,7 +598,7 @@ void CminusfBuilder::visit(ASTCall &node) {
cur_value
->
get_type
()
->
get_array_element_type
()))
error_exit
(
"expected right pointer type"
);
// int[] to int* or float[] to flot*
cur_value
=
builder
->
create_gep
(
cur_value
,
{
0
,
0
});
cur_value
=
builder
->
create_gep
(
cur_value
,
{
CONST_INT
(
0
),
CONST_INT
(
0
)
});
}
else
if
(
param_type
->
is_integer_type
()
or
param_type
->
is_float_type
())
{
// need type cast between int and float
if
(
not
cur_value
->
get_type
()
->
is_integer_type
()
and
not
cur_value
->
get_type
()
->
is_float_type
())
...
...
src/cminusfc/cminusfc.cpp
View file @
6d308da4
#include "cminusf_builder.hpp"
#include "logging.hpp"
#include <fstream>
#include <iostream>
#include <memory>
#include "logging.hpp"
using
namespace
std
::
literals
::
string_literals
;
void
print_help
(
std
::
string
exe_name
)
{
...
...
@@ -83,7 +82,13 @@ int main(int argc, char **argv) {
auto
s
=
parse
(
input_path
.
c_str
());
auto
a
=
AST
(
s
);
CminusfBuilder
builder
;
a
.
run_visitor
(
builder
);
try
{
a
.
run_visitor
(
builder
);
}
catch
(
std
::
exception
e
)
{
std
::
cout
<<
"Exception catched:
\n\t
"
<<
e
.
what
()
<<
std
::
endl
;
std
::
cout
<<
"Already generated IR code:
\n
"
<<
builder
.
getModule
()
->
print
();
return
-
1
;
}
auto
m
=
builder
.
getModule
();
...
...
tests/3-ir-gen/testcases/lv1/assign_int_array_local.cminus
View file @
6d308da4
void main(void) {
int a[10];
a[3] = 1234;
/* a[3] = 1234; */
output(a[3]);
return;
}
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