Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
latte
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zygzagZ
latte
Commits
bc58b2c2
Commit
bc58b2c2
authored
Jan 07, 2021
by
zygzagZ
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
known alive variables after every quadruple
parent
a83f92b3
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
129 additions
and
9 deletions
+129
-9
Compiler.cpp
src/Compiler.cpp
+33
-3
BasicBlock.h
src/codeGen/BasicBlock.h
+9
-0
Quadruple.h
src/codeGen/Quadruple.h
+27
-1
QuadrupleGenerator.cpp
src/codeGen/QuadrupleGenerator.cpp
+7
-5
RegisterAllocator.cpp
src/codeGen/RegisterAllocator.cpp
+31
-0
RegisterAllocator.h
src/codeGen/RegisterAllocator.h
+2
-0
setOverloads.h
src/codeGen/setOverloads.h
+20
-0
No files found.
src/Compiler.cpp
View file @
bc58b2c2
...
...
@@ -57,12 +57,23 @@ void Compiler::compileFunction(const FunctionInfoPtr& f) {
auto
quadEnv
=
quadGen
.
compileFunction
(
f
);
RegisterAllocator
regGen
(
quadEnv
);
RegisterAllocator
regGen
=
RegisterAllocator
(
quadEnv
);
regGen
.
allocate
();
int
blkNo
=
0
;
for
(
const
auto
&
b
:
quadEnv
.
blocks
)
{
buf
<<
"------------------------------
\n
"
;
buf
<<
"blok "
<<
blkNo
<<
" "
<<
b
->
getName
()
<<
endl
;
/*buf << "in: ";
for (auto in : b->in) {
buf << in->getName() << " ";
}
buf << "\nouts: ";
for (auto out : b->out) {
buf << out->getName() << " ";
}
buf << "\n";*/
if
(
!
b
->
flow
.
in
.
empty
())
{
buf
<<
"in: "
;
for
(
const
auto
&
v
:
b
->
flow
.
in
)
{
...
...
@@ -70,10 +81,28 @@ void Compiler::compileFunction(const FunctionInfoPtr& f) {
}
buf
<<
"
\n
----------
\n
"
;
}
if
(
!
b
->
flow
.
def
.
empty
())
{
buf
<<
"def: "
;
for
(
const
auto
&
v
:
b
->
flow
.
def
)
{
buf
<<
v
->
name
<<
", "
;
}
buf
<<
"
\n
----------
\n
"
;
}
for
(
const
auto
&
q
:
b
->
quads
)
{
auto
ret
=
q
->
toString
();
if
(
!
ret
.
empty
())
buf
<<
ret
<<
endl
;
if
(
!
ret
.
empty
())
{
buf
<<
ret
<<
" alive: "
;
for
(
auto
var
:
q
->
aliveAfter
)
buf
<<
var
->
name
<<
" "
;
buf
<<
endl
;
}
}
if
(
!
b
->
flow
.
use
.
empty
())
{
buf
<<
"----------
\n
use: "
;
for
(
const
auto
&
v
:
b
->
flow
.
use
)
{
buf
<<
v
->
name
<<
", "
;
}
buf
<<
"
\n
"
;
}
if
(
!
b
->
flow
.
out
.
empty
())
{
buf
<<
"----------
\n
out: "
;
...
...
@@ -82,5 +111,6 @@ void Compiler::compileFunction(const FunctionInfoPtr& f) {
}
buf
<<
"
\n
"
;
}
blkNo
++
;
}
}
src/codeGen/BasicBlock.h
View file @
bc58b2c2
...
...
@@ -49,6 +49,15 @@ public:
after
->
phi
->
phi
.
emplace_back
();
}
std
::
string
getName
()
const
{
for
(
const
auto
&
q
:
quads
)
{
if
(
auto
l
=
dynamic_pointer_cast
<
QLabel
>
(
q
))
{
return
l
->
label
;
}
}
return
"?"
;
}
void
addJumpInitQuad
(
const
QuadruplePtr
&
q
)
{
afterInit
.
push_back
(
q
);
}
...
...
src/codeGen/Quadruple.h
View file @
bc58b2c2
...
...
@@ -41,6 +41,7 @@ public:
int
lineno
;
int
index
;
BasicBlockPtr
block
;
set
<
VariablePtr
>
aliveAfter
;
virtual
~
Quadruple
()
=
default
;
virtual
std
::
string
toString
()
const
{
return
to_string
(
lineno
)
+
"
\t
"
;
};
...
...
@@ -53,6 +54,7 @@ public:
}
};
virtual
vector
<
VariablePtr
>
vars
()
const
{
return
{};
};
virtual
vector
<
VariablePtr
>
definitions
()
const
{
return
{};
};
};
class
QWriteVar
:
public
Quadruple
{
...
...
@@ -65,6 +67,11 @@ public:
Quadruple
::
useVariables
();
if
(
loc
)
loc
->
writes
.
emplace_back
(
shared_from_this
());
}
vector
<
VariablePtr
>
definitions
()
const
override
{
auto
ret
=
Quadruple
::
definitions
();
if
(
loc
)
ret
.
emplace_back
(
loc
);
return
ret
;
};
};
class
QLabel
:
public
Quadruple
{
...
...
@@ -161,7 +168,14 @@ public:
void
useVariables
()
override
{
Quadruple
::
useVariables
();
if
(
param
&&
num
<
0
)
param
->
writes
.
emplace_back
(
shared_from_this
());
}};
}
vector
<
VariablePtr
>
definitions
()
const
override
{
auto
ret
=
Quadruple
::
definitions
();
if
(
param
&&
num
<
0
)
ret
.
emplace_back
(
param
);
return
ret
;
};
};
class
QCall
:
public
QWriteVar
{
public:
...
...
@@ -300,6 +314,18 @@ public:
}
}
vector
<
VariablePtr
>
definitions
()
const
override
{
auto
ret
=
Quadruple
::
definitions
();
set
<
VariablePtr
>
written
;
for
(
const
auto
&
b
:
phi
)
{
for
(
const
auto
&
v
:
b
)
{
written
.
emplace
(
v
.
first
);
}
}
std
::
copy
(
written
.
begin
(),
written
.
end
(),
std
::
back_inserter
(
ret
));
return
ret
;
};
std
::
string
toString
()
const
override
{
if
(
phi
.
size
()
<
2
)
return
""
;
std
::
string
ret
=
Quadruple
::
toString
()
+
"phi "
;
...
...
src/codeGen/QuadrupleGenerator.cpp
View file @
bc58b2c2
...
...
@@ -381,6 +381,7 @@ void QuadrupleGenerator::visitCondElse(CondElse *p) {
}
void
QuadrupleGenerator
::
compileCond
(
Expr
*
expr_
,
Stmt
*
stmt_1
,
Stmt
*
stmt_2
)
{
// TODO: stmty nie składają się z jednego bloku tylko z wielu
auto
elseBranch
=
make_shared
<
QLabel
>
(
"else"
);
auto
after
=
make_shared
<
QLabel
>
(
"end_else"
);
...
...
@@ -451,10 +452,11 @@ void QuadrupleGenerator::visitWhile(While *expr) {
// env2 starts with hooked variables
auto
env2
=
captureEnv
();
auto
loopFirstBlock
=
block
;
expr
->
stmt_
->
accept
(
this
);
auto
loopBlock
=
flushBasicBlock
();
// jump <- cond
auto
loop
Last
Block
=
flushBasicBlock
();
// jump <- cond
auto
condBlock
=
block
;
addQuad
(
cond
);
...
...
@@ -464,8 +466,8 @@ void QuadrupleGenerator::visitWhile(While *expr) {
env1
.
revert
();
beforeBlock
->
append
(
condBlock
);
condBlock
->
append
(
loopBlock
);
loopBlock
->
append
(
condBlock
);
condBlock
->
append
(
loop
First
Block
);
loop
Last
Block
->
append
(
condBlock
);
for
(
const
auto
&
p
:
env2
.
changes
)
{
auto
info
=
p
.
first
;
...
...
@@ -477,9 +479,9 @@ void QuadrupleGenerator::visitWhile(While *expr) {
// transition from pre-hook to hooked var [before -> cond]
auto
orig
=
env1
.
changes
[
info
].
first
;
condBlock
->
addPhi
(
beforeBlock
,
hooked
,
orig
);
condBlock
->
addPhi
(
loopBlock
,
hooked
,
looped
);
condBlock
->
addPhi
(
loop
Last
Block
,
hooked
,
looped
);
// loopBlock doesn't need phi, it has only one incoming block
// loop
Last
Block doesn't need phi, it has only one incoming block
}
// expr uses pre-hook variables iff unused in loop
...
...
src/codeGen/RegisterAllocator.cpp
View file @
bc58b2c2
...
...
@@ -42,4 +42,35 @@ void RegisterAllocator::analyseLive() {
void
RegisterAllocator
::
allocate
()
{
analyseLive
();
buildGraph
();
}
void
RegisterAllocator
::
buildGraph
()
{
for
(
const
auto
&
b
:
blocks
)
{
set
<
VariablePtr
>
alive
=
b
->
flow
.
in
;
for
(
const
auto
&
q
:
b
->
quads
)
{
alive
+=
to_set
(
q
->
definitions
());
vector
<
VariablePtr
>
dead
;
for
(
const
auto
&
var
:
alive
)
{
// this quad is the last quad that uses var
if
((
var
->
uses
.
empty
()
||
var
->
uses
.
back
()
==
q
)
&&
!
b
->
flow
.
out
.
contains
(
var
))
{
dead
.
emplace_back
(
var
);
}
}
alive
-=
to_set
(
dead
);
q
->
aliveAfter
=
alive
;
for
(
auto
i
:
alive
)
{
for
(
auto
j
:
alive
)
{
if
(
i
!=
j
)
{
if
(
i
.
get
()
>
j
.
get
())
{
swap
(
i
,
j
);
}
graphEdges
.
insert
(
make_pair
(
i
,
j
));
}
}
}
}
}
}
src/codeGen/RegisterAllocator.h
View file @
bc58b2c2
...
...
@@ -27,10 +27,12 @@ public:
void
processQCall
(
shared_ptr
<
QCall
>
q
);
void
allocate
();
void
buildGraph
();
private:
void
analyseLive
();
vector
<
BasicBlockPtr
>
blocks
;
vector
<
VariablePtr
>
vars
;
set
<
pair
<
VariablePtr
,
VariablePtr
>>
graphEdges
;
};
...
...
src/codeGen/setOverloads.h
View file @
bc58b2c2
...
...
@@ -31,6 +31,18 @@ set<T>& operator+=(set<T> &l, const set<T> &r) {
return
l
;
}
template
<
typename
T
>
set
<
T
>&
operator
-=
(
set
<
T
>
&
l
,
const
set
<
T
>
&
r
)
{
auto
diff
=
l
-
r
;
return
l
=
diff
;
}
template
<
typename
T
>
set
<
T
>&
operator
^=
(
set
<
T
>
&
l
,
const
set
<
T
>
&
r
)
{
auto
diff
=
l
^
r
;
return
l
=
diff
;
}
template
<
typename
T
>
bool
operator
==
(
const
set
<
T
>
&
l
,
const
set
<
T
>
&
r
)
{
return
(
r
^
l
).
empty
();
...
...
@@ -41,4 +53,12 @@ bool operator!=(const set<T> &l, const set<T> &r) {
return
!
(
r
==
l
);
}
template
<
typename
C
>
set
<
typename
C
::
value_type
>
to_set
(
const
C
&
v
)
{
set
<
typename
C
::
value_type
>
res
;
for
(
auto
&
i
:
v
)
res
.
emplace
(
i
);
return
res
;
}
#endif //ZAD2_SETOVERLOADS_H
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