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
eda3505d
Commit
eda3505d
authored
Jan 27, 2023
by
lxq
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finish most integer instructions
parent
793fd3dd
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
258 additions
and
41 deletions
+258
-41
include/codegen.hpp
include/codegen.hpp
+46
-13
src/codegen/codegen.cpp
src/codegen/codegen.cpp
+212
-28
No files found.
include/codegen.hpp
View file @
eda3505d
#ifndef CODEGEN_HPP
#define CODEGEN_HPP
#include "BasicBlock.h"
#include "Function.h"
#include "Instruction.h"
#include "Module.h"
...
...
@@ -8,8 +9,11 @@
#include "logging.hpp"
#include <map>
#include <ostream>
#define STACK_ALIGN(x) (((x / 16) + (x % 16 ? 1 : 0)) * 16)
#define ALIGN(x, align) (((x / align) + (x % align ? 1 : 0)) * align)
// #define STACK_ALIGN(x) (((x / 16) + (x % 16 ? 1 : 0)) * 16)
#define STACK_ALIGN(x) ALIGN(x, 16)
using
std
::
map
;
using
std
::
string
;
...
...
@@ -32,18 +36,23 @@ class CodeGen {
void
run
();
private:
void
IR2assem
(
Instruction
&
);
void
IR2assem
(
ReturnInst
*
);
void
IR2assem
(
LoadInst
*
);
void
IR2assem
(
StoreInst
*
);
void
IR2assem
(
BranchInst
*
)
{}
void
IR2assem
(
BinaryInst
*
)
{}
void
IR2assem
(
AllocaInst
*
)
{}
void
IR2assem
(
PhiInst
*
)
{}
void
IR2assem
(
CallInst
*
)
{}
void
IR2assem
(
ReturnInst
*
);
void
IR2assem
(
Instruction
&
);
void
IR2assem
(
GetElementPtrInst
*
);
void
IR2assem
(
CallInst
*
);
void
IR2assem
(
BranchInst
*
);
// The Instructions below will do nothing
void
IR2assem
(
AllocaInst
*
)
{}
// integration with BranchInst
void
IR2assem
(
CmpInst
*
)
{}
void
IR2assem
(
ZextInst
*
)
{}
void
IR2assem
(
BinaryInst
*
);
void
IR2assem
(
PhiInst
*
)
{}
void
IR2assem
(
FCmpInst
*
)
{}
void
IR2assem
(
FpToSiInst
*
)
{}
void
IR2assem
(
SiToFpInst
*
)
{}
...
...
@@ -54,11 +63,16 @@ class CodeGen {
// - for global variables and pointers from alloca and GEP, read through
// address
// only use register a_id and t_
void
value2reg
(
Value
*
,
int
id
=
0
);
void
value2reg
(
Value
*
,
int
id
=
0
,
bool
is_float
=
false
);
// load the content in ptr to specified register.
// only use register a_id and t_
void
ptrContent2reg
(
Value
*
,
int
id
=
0
);
void
compute_arg_info
(
Function
*
);
string
bool2branch
(
Instruction
*
);
string
label_in_assem
(
BasicBlock
*
bb
)
{
return
cur_func
->
get_name
()
+
bb
->
get_name
().
substr
(
5
);
}
int
typeLen
(
Type
*
type
)
{
if
(
type
->
is_float_type
())
return
4
;
...
...
@@ -73,9 +87,17 @@ class CodeGen {
auto
arr_tp
=
static_cast
<
ArrayType
*>
(
type
);
int
n
=
arr_tp
->
get_num_of_elements
();
return
n
*
typeLen
(
arr_tp
->
get_element_type
());
}
else
}
else
{
assert
(
false
&&
"unexpected case while computing type-length"
);
}
}
void
back2stack
(
Instruction
*
instr
,
string
reg
=
"$a0"
)
{
// std::cerr << instr->print() << std::endl;
string
suff
=
suffix
(
typeLen
(
instr
->
get_type
()));
string
addr
=
"$fp, -"
+
std
::
to_string
(
off
[
instr
]);
output
.
push_back
(
"st"
+
suff
+
" "
+
reg
+
" "
+
addr
);
}
string
suffix
(
int
len
)
{
switch
(
len
)
{
...
...
@@ -91,8 +113,19 @@ class CodeGen {
assert
(
false
&&
"no such suffix"
);
}
std
::
map
<
Value
*
,
unsigned
int
>
off
;
unsigned
int
N
;
bool
no_stack_alloca
(
Instruction
*
instr
)
{
if
(
instr
->
is_void
())
return
true
;
if
(
instr
->
is_cmp
()
or
instr
->
is_zext
())
return
true
;
return
false
;
}
std
::
map
<
Value
*
,
unsigned
int
>
off
;
// to $fp
std
::
map
<
Function
*
,
std
::
map
<
int
,
int
>>
func_arg_off
;
// to $sp
std
::
map
<
Function
*
,
int
>
func_arg_N
;
unsigned
int
stackN
;
Function
*
cur_func
;
...
...
src/codegen/codegen.cpp
View file @
eda3505d
This diff is collapsed.
Click to expand it.
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