Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
2
2025ustc-jianmu-compiler
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
43
Merge Requests
43
CI / CD
CI / CD
Pipelines
Jobs
Schedules
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
compiler_staff
2025ustc-jianmu-compiler
Commits
c396901a
Commit
c396901a
authored
Nov 26, 2025
by
Yang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add names api
parent
a6d37a3d
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
236 additions
and
152 deletions
+236
-152
include/lightir/BasicBlock.hpp
include/lightir/BasicBlock.hpp
+4
-1
include/lightir/Function.hpp
include/lightir/Function.hpp
+7
-0
include/lightir/IRBuilder.hpp
include/lightir/IRBuilder.hpp
+2
-2
include/lightir/Instruction.hpp
include/lightir/Instruction.hpp
+47
-44
include/lightir/Names.hpp
include/lightir/Names.hpp
+27
-0
src/cminusfc/cminusf_builder.cpp
src/cminusfc/cminusf_builder.cpp
+6
-6
src/lightir/BasicBlock.cpp
src/lightir/BasicBlock.cpp
+10
-7
src/lightir/CMakeLists.txt
src/lightir/CMakeLists.txt
+1
-1
src/lightir/Function.cpp
src/lightir/Function.cpp
+10
-1
src/lightir/Instruction.cpp
src/lightir/Instruction.cpp
+96
-90
src/lightir/Names.cpp
src/lightir/Names.cpp
+26
-0
No files found.
include/lightir/BasicBlock.hpp
View file @
c396901a
...
@@ -81,11 +81,14 @@ class BasicBlock : public Value {
...
@@ -81,11 +81,14 @@ class BasicBlock : public Value {
// 用于 lldb 调试生成 summary
// 用于 lldb 调试生成 summary
std
::
string
safe_print
()
const
;
std
::
string
safe_print
()
const
;
private:
private:
explicit
BasicBlock
(
Module
*
m
,
const
std
::
string
&
name
,
Function
*
parent
);
explicit
BasicBlock
(
const
Module
*
m
,
const
std
::
string
&
name
,
Function
*
parent
);
std
::
list
<
BasicBlock
*>
pre_bbs_
;
std
::
list
<
BasicBlock
*>
pre_bbs_
;
std
::
list
<
BasicBlock
*>
succ_bbs_
;
std
::
list
<
BasicBlock
*>
succ_bbs_
;
std
::
list
<
Instruction
*>
instr_list_
;
std
::
list
<
Instruction
*>
instr_list_
;
Function
*
parent_
;
Function
*
parent_
;
};
};
extern
Names
GLOBAL_BASICBLOCK_NAMES_
;
\ No newline at end of file
include/lightir/Function.hpp
View file @
c396901a
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
#include <list>
#include <list>
#include <memory>
#include <memory>
#include "Names.hpp"
class
Module
;
class
Module
;
class
Argument
;
class
Argument
;
class
Type
;
class
Type
;
...
@@ -49,6 +51,11 @@ class Function : public Value {
...
@@ -49,6 +51,11 @@ class Function : public Value {
// 用于 lldb 调试生成 summary
// 用于 lldb 调试生成 summary
std
::
string
safe_print
()
const
;
std
::
string
safe_print
()
const
;
// 用于给 basicblock 分配名称
Names
names4blocks_
;
Names
names4insts_
;
private:
private:
std
::
list
<
BasicBlock
*>
basic_blocks_
;
std
::
list
<
BasicBlock
*>
basic_blocks_
;
std
::
list
<
Argument
>
arguments_
;
std
::
list
<
Argument
>
arguments_
;
...
...
include/lightir/IRBuilder.hpp
View file @
c396901a
...
@@ -105,9 +105,9 @@ class IRBuilder {
...
@@ -105,9 +105,9 @@ class IRBuilder {
return
LoadInst
::
create_load
(
ptr
,
this
->
BB_
);
return
LoadInst
::
create_load
(
ptr
,
this
->
BB_
);
}
}
AllocaInst
*
create_alloca
(
Type
*
ty
)
const
AllocaInst
*
create_alloca
(
Type
*
ty
,
const
std
::
string
&
name
=
""
)
const
{
{
return
AllocaInst
::
create_alloca
(
ty
,
this
->
BB_
->
get_entry_block_of_same_function
());
return
AllocaInst
::
create_alloca
(
ty
,
this
->
BB_
->
get_entry_block_of_same_function
()
,
name
);
}
}
ZextInst
*
create_zext
(
Value
*
val
,
Type
*
ty
)
const
ZextInst
*
create_zext
(
Value
*
val
,
Type
*
ty
)
const
...
...
include/lightir/Instruction.hpp
View file @
c396901a
#pragma once
#pragma once
#include "Names.hpp"
#include "Type.hpp"
#include "Type.hpp"
#include "User.hpp"
#include "User.hpp"
...
@@ -52,7 +53,7 @@ class Instruction : public User {
...
@@ -52,7 +53,7 @@ class Instruction : public User {
};
};
/* @parent: if parent!=nullptr, auto insert to bb
/* @parent: if parent!=nullptr, auto insert to bb
* @ty: result type */
* @ty: result type */
Instruction
(
Type
*
ty
,
OpID
id
,
BasicBlock
*
parent
=
nullptr
);
Instruction
(
Type
*
ty
,
OpID
id
,
const
std
::
string
&
name
,
BasicBlock
*
parent
=
nullptr
);
~
Instruction
()
override
;
~
Instruction
()
override
;
Instruction
(
const
Instruction
&
other
)
=
delete
;
Instruction
(
const
Instruction
&
other
)
=
delete
;
...
@@ -121,13 +122,13 @@ class Instruction : public User {
...
@@ -121,13 +122,13 @@ class Instruction : public User {
class
IBinaryInst
:
public
Instruction
{
class
IBinaryInst
:
public
Instruction
{
private:
private:
IBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
IBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
IBinaryInst
*
create_add
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
IBinaryInst
*
create_add
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
IBinaryInst
*
create_sub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
IBinaryInst
*
create_sub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
IBinaryInst
*
create_mul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
IBinaryInst
*
create_mul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
IBinaryInst
*
create_sdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
IBinaryInst
*
create_sdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
std
::
string
print
()
override
;
std
::
string
print
()
override
;
};
};
...
@@ -135,13 +136,13 @@ class IBinaryInst : public Instruction {
...
@@ -135,13 +136,13 @@ class IBinaryInst : public Instruction {
class
FBinaryInst
:
public
Instruction
{
class
FBinaryInst
:
public
Instruction
{
private:
private:
FBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
FBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
FBinaryInst
*
create_fadd
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FBinaryInst
*
create_fadd
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FBinaryInst
*
create_fsub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FBinaryInst
*
create_fsub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FBinaryInst
*
create_fmul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FBinaryInst
*
create_fmul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FBinaryInst
*
create_fdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FBinaryInst
*
create_fdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
std
::
string
print
()
override
;
std
::
string
print
()
override
;
};
};
...
@@ -149,16 +150,16 @@ class FBinaryInst : public Instruction {
...
@@ -149,16 +150,16 @@ class FBinaryInst : public Instruction {
class
ICmpInst
:
public
Instruction
{
class
ICmpInst
:
public
Instruction
{
private:
private:
ICmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
);
ICmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
ICmpInst
*
create_ge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
ICmpInst
*
create_ge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
ICmpInst
*
create_gt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
ICmpInst
*
create_gt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
ICmpInst
*
create_le
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
ICmpInst
*
create_le
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
ICmpInst
*
create_lt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
ICmpInst
*
create_lt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
ICmpInst
*
create_eq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
ICmpInst
*
create_eq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
ICmpInst
*
create_ne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
ICmpInst
*
create_ne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
std
::
string
print
()
override
;
std
::
string
print
()
override
;
};
};
...
@@ -166,15 +167,15 @@ class ICmpInst : public Instruction {
...
@@ -166,15 +167,15 @@ class ICmpInst : public Instruction {
class
FCmpInst
:
public
Instruction
{
class
FCmpInst
:
public
Instruction
{
private:
private:
FCmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
);
FCmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
FCmpInst
*
create_fge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FCmpInst
*
create_fge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FCmpInst
*
create_fgt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FCmpInst
*
create_fgt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FCmpInst
*
create_fle
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FCmpInst
*
create_fle
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FCmpInst
*
create_flt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FCmpInst
*
create_flt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FCmpInst
*
create_feq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FCmpInst
*
create_feq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FCmpInst
*
create_fne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
);
static
FCmpInst
*
create_fne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
std
::
string
print
()
override
;
std
::
string
print
()
override
;
};
};
...
@@ -182,11 +183,11 @@ class FCmpInst : public Instruction {
...
@@ -182,11 +183,11 @@ class FCmpInst : public Instruction {
class
CallInst
:
public
Instruction
{
class
CallInst
:
public
Instruction
{
protected:
protected:
CallInst
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
BasicBlock
*
bb
);
CallInst
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
CallInst
*
create_call
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
static
CallInst
*
create_call
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
BasicBlock
*
bb
);
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
FunctionType
*
get_function_type
()
const
;
FunctionType
*
get_function_type
()
const
;
std
::
string
print
()
override
;
std
::
string
print
()
override
;
...
@@ -232,12 +233,12 @@ class ReturnInst : public Instruction {
...
@@ -232,12 +233,12 @@ class ReturnInst : public Instruction {
class
GetElementPtrInst
:
public
Instruction
{
class
GetElementPtrInst
:
public
Instruction
{
private:
private:
GetElementPtrInst
(
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
BasicBlock
*
bb
);
GetElementPtrInst
(
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
Type
*
get_element_type
(
const
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
);
static
Type
*
get_element_type
(
const
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
const
std
::
string
&
name
=
""
);
static
GetElementPtrInst
*
create_gep
(
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
static
GetElementPtrInst
*
create_gep
(
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
BasicBlock
*
bb
);
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
Type
*
get_element_type
()
const
;
Type
*
get_element_type
()
const
;
std
::
string
print
()
override
;
std
::
string
print
()
override
;
...
@@ -258,10 +259,10 @@ class StoreInst : public Instruction {
...
@@ -258,10 +259,10 @@ class StoreInst : public Instruction {
class
LoadInst
:
public
Instruction
{
class
LoadInst
:
public
Instruction
{
LoadInst
(
Value
*
ptr
,
BasicBlock
*
bb
);
LoadInst
(
Value
*
ptr
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
LoadInst
*
create_load
(
Value
*
ptr
,
BasicBlock
*
bb
);
static
LoadInst
*
create_load
(
Value
*
ptr
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
Value
*
get_lval
()
const
{
return
this
->
get_operand
(
0
);
}
Value
*
get_lval
()
const
{
return
this
->
get_operand
(
0
);
}
Type
*
get_load_type
()
const
{
return
get_type
();
}
Type
*
get_load_type
()
const
{
return
get_type
();
}
...
@@ -271,11 +272,11 @@ class LoadInst : public Instruction {
...
@@ -271,11 +272,11 @@ class LoadInst : public Instruction {
class
AllocaInst
:
public
Instruction
{
class
AllocaInst
:
public
Instruction
{
AllocaInst
(
Type
*
ty
,
BasicBlock
*
bb
);
AllocaInst
(
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
// 新特性:指令表分为 {alloca, phi | other inst} 两段,创建和向基本块插入 alloca 和 phi,都只会插在第一段,它们在常规指令前面
// 新特性:指令表分为 {alloca, phi | other inst} 两段,创建和向基本块插入 alloca 和 phi,都只会插在第一段,它们在常规指令前面
static
AllocaInst
*
create_alloca
(
Type
*
ty
,
BasicBlock
*
bb
);
static
AllocaInst
*
create_alloca
(
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
Type
*
get_alloca_type
()
const
{
Type
*
get_alloca_type
()
const
{
return
get_type
()
->
get_pointer_element_type
();
return
get_type
()
->
get_pointer_element_type
();
...
@@ -287,11 +288,11 @@ class AllocaInst : public Instruction {
...
@@ -287,11 +288,11 @@ class AllocaInst : public Instruction {
class
ZextInst
:
public
Instruction
{
class
ZextInst
:
public
Instruction
{
private:
private:
ZextInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
);
ZextInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
ZextInst
*
create_zext
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
);
static
ZextInst
*
create_zext
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
ZextInst
*
create_zext_to_i32
(
Value
*
val
,
BasicBlock
*
bb
);
static
ZextInst
*
create_zext_to_i32
(
Value
*
val
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
Type
*
get_dest_type
()
const
{
return
get_type
();
}
Type
*
get_dest_type
()
const
{
return
get_type
();
}
...
@@ -301,11 +302,11 @@ class ZextInst : public Instruction {
...
@@ -301,11 +302,11 @@ class ZextInst : public Instruction {
class
FpToSiInst
:
public
Instruction
{
class
FpToSiInst
:
public
Instruction
{
private:
private:
FpToSiInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
);
FpToSiInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
FpToSiInst
*
create_fptosi
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
);
static
FpToSiInst
*
create_fptosi
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
static
FpToSiInst
*
create_fptosi_to_i32
(
Value
*
val
,
BasicBlock
*
bb
);
static
FpToSiInst
*
create_fptosi_to_i32
(
Value
*
val
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
Type
*
get_dest_type
()
const
{
return
get_type
();
}
Type
*
get_dest_type
()
const
{
return
get_type
();
}
...
@@ -315,10 +316,10 @@ class FpToSiInst : public Instruction {
...
@@ -315,10 +316,10 @@ class FpToSiInst : public Instruction {
class
SiToFpInst
:
public
Instruction
{
class
SiToFpInst
:
public
Instruction
{
private:
private:
SiToFpInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
);
SiToFpInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
static
SiToFpInst
*
create_sitofp
(
Value
*
val
,
BasicBlock
*
bb
);
static
SiToFpInst
*
create_sitofp
(
Value
*
val
,
BasicBlock
*
bb
,
const
std
::
string
&
name
=
""
);
Type
*
get_dest_type
()
const
{
return
get_type
();
}
Type
*
get_dest_type
()
const
{
return
get_type
();
}
...
@@ -329,13 +330,13 @@ class PhiInst : public Instruction {
...
@@ -329,13 +330,13 @@ class PhiInst : public Instruction {
private:
private:
PhiInst
(
Type
*
ty
,
const
std
::
vector
<
Value
*>&
vals
,
PhiInst
(
Type
*
ty
,
const
std
::
vector
<
Value
*>&
vals
,
const
std
::
vector
<
BasicBlock
*>&
val_bbs
,
BasicBlock
*
bb
);
const
std
::
vector
<
BasicBlock
*>&
val_bbs
,
BasicBlock
*
bb
,
const
std
::
string
&
name
);
public:
public:
// 新特性:指令表分为 {alloca, phi | other inst} 两段,创建和向基本块插入 alloca 和 phi,都只会插在第一段,它们在常规指令前面
// 新特性:指令表分为 {alloca, phi | other inst} 两段,创建和向基本块插入 alloca 和 phi,都只会插在第一段,它们在常规指令前面
static
PhiInst
*
create_phi
(
Type
*
ty
,
BasicBlock
*
bb
,
static
PhiInst
*
create_phi
(
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
vector
<
Value
*>&
vals
=
{},
const
std
::
vector
<
Value
*>&
vals
=
{},
const
std
::
vector
<
BasicBlock
*>&
val_bbs
=
{});
const
std
::
vector
<
BasicBlock
*>&
val_bbs
=
{}
,
const
std
::
string
&
name
=
""
);
void
add_phi_pair_operand
(
Value
*
val
,
Value
*
pre_bb
)
{
void
add_phi_pair_operand
(
Value
*
val
,
Value
*
pre_bb
)
{
this
->
add_operand
(
val
);
this
->
add_operand
(
val
);
...
@@ -354,3 +355,5 @@ class PhiInst : public Instruction {
...
@@ -354,3 +355,5 @@ class PhiInst : public Instruction {
std
::
string
print
()
override
;
std
::
string
print
()
override
;
};
};
extern
Names
GLOBAL_INSTRUCTION_NAMES_
;
\ No newline at end of file
include/lightir/Names.hpp
0 → 100644
View file @
c396901a
#ifndef NAMES_HPP
#define NAMES_HPP
#include <string>
#include <unordered_map>
class
Names
{
public:
explicit
Names
(
bool
use_underline
,
std
::
string
prefix
,
std
::
string
append
)
:
use_underline_
(
use_underline
),
default_prefix_used_count_
(
0
),
default_prefix_
(
std
::
move
(
prefix
)),
appended_prefix_
(
std
::
move
(
append
))
{
}
// 获得一个 Names 内唯一的名称,会保持唯一的同时尽可能的与 names 类似
std
::
string
get_name
(
std
::
string
name
);
// 获得一个 Names 内唯一的名称
std
::
string
get_name
();
private:
bool
use_underline_
;
int
default_prefix_used_count_
;
std
::
string
default_prefix_
;
std
::
string
appended_prefix_
;
std
::
unordered_map
<
std
::
string
,
int
>
allocated_
;
};
#endif // !NAMES_HPP
src/cminusfc/cminusf_builder.cpp
View file @
c396901a
...
@@ -91,7 +91,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) {
...
@@ -91,7 +91,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) {
auto
nowBB
=
builder
->
get_insert_block
();
auto
nowBB
=
builder
->
get_insert_block
();
auto
entryBB
=
context
.
func
->
get_entry_block
();
auto
entryBB
=
context
.
func
->
get_entry_block
();
builder
->
set_insert_point
(
entryBB
);
builder
->
set_insert_point
(
entryBB
);
auto
*
var
=
builder
->
create_alloca
(
var_type
);
auto
*
var
=
builder
->
create_alloca
(
var_type
,
node
.
id
);
builder
->
set_insert_point
(
nowBB
);
builder
->
set_insert_point
(
nowBB
);
scope
.
push
(
node
.
id
,
var
);
scope
.
push
(
node
.
id
,
var
);
}
else
{
}
else
{
...
@@ -99,7 +99,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) {
...
@@ -99,7 +99,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) {
auto
entryBB
=
context
.
func
->
get_entry_block
();
auto
entryBB
=
context
.
func
->
get_entry_block
();
builder
->
set_insert_point
(
entryBB
);
builder
->
set_insert_point
(
entryBB
);
auto
*
array_type
=
ArrayType
::
get
(
var_type
,
node
.
num
->
i_val
);
auto
*
array_type
=
ArrayType
::
get
(
var_type
,
node
.
num
->
i_val
);
auto
*
var
=
builder
->
create_alloca
(
array_type
);
auto
*
var
=
builder
->
create_alloca
(
array_type
,
node
.
id
);
builder
->
set_insert_point
(
nowBB
);
builder
->
set_insert_point
(
nowBB
);
scope
.
push
(
node
.
id
,
var
);
scope
.
push
(
node
.
id
,
var
);
}
}
...
@@ -149,18 +149,18 @@ Value* CminusfBuilder::visit(ASTFunDeclaration &node) {
...
@@ -149,18 +149,18 @@ Value* CminusfBuilder::visit(ASTFunDeclaration &node) {
if
(
node
.
params
[
i
]
->
isarray
)
{
if
(
node
.
params
[
i
]
->
isarray
)
{
Value
*
array_alloc
;
Value
*
array_alloc
;
if
(
node
.
params
[
i
]
->
type
==
TYPE_INT
)
{
if
(
node
.
params
[
i
]
->
type
==
TYPE_INT
)
{
array_alloc
=
builder
->
create_alloca
(
INT32PTR_T
);
array_alloc
=
builder
->
create_alloca
(
INT32PTR_T
,
node
.
params
[
i
]
->
id
);
}
else
{
}
else
{
array_alloc
=
builder
->
create_alloca
(
FLOATPTR_T
);
array_alloc
=
builder
->
create_alloca
(
FLOATPTR_T
,
node
.
params
[
i
]
->
id
);
}
}
builder
->
create_store
(
args
[
i
],
array_alloc
);
builder
->
create_store
(
args
[
i
],
array_alloc
);
scope
.
push
(
node
.
params
[
i
]
->
id
,
array_alloc
);
scope
.
push
(
node
.
params
[
i
]
->
id
,
array_alloc
);
}
else
{
}
else
{
Value
*
alloc
;
Value
*
alloc
;
if
(
node
.
params
[
i
]
->
type
==
TYPE_INT
)
{
if
(
node
.
params
[
i
]
->
type
==
TYPE_INT
)
{
alloc
=
builder
->
create_alloca
(
INT32_T
);
alloc
=
builder
->
create_alloca
(
INT32_T
,
node
.
params
[
i
]
->
id
);
}
else
{
}
else
{
alloc
=
builder
->
create_alloca
(
FLOAT_T
);
alloc
=
builder
->
create_alloca
(
FLOAT_T
,
node
.
params
[
i
]
->
id
);
}
}
builder
->
create_store
(
args
[
i
],
alloc
);
builder
->
create_store
(
args
[
i
],
alloc
);
scope
.
push
(
node
.
params
[
i
]
->
id
,
alloc
);
scope
.
push
(
node
.
params
[
i
]
->
id
,
alloc
);
...
...
src/lightir/BasicBlock.cpp
View file @
c396901a
...
@@ -7,9 +7,13 @@
...
@@ -7,9 +7,13 @@
#include <cassert>
#include <cassert>
BasicBlock
::
BasicBlock
(
Module
*
m
,
const
std
::
string
&
name
=
""
,
#include "Names.hpp"
Function
*
parent
=
nullptr
)
:
Value
(
m
->
get_label_type
(),
name
),
parent_
(
parent
)
{
BasicBlock
::
BasicBlock
(
const
Module
*
m
,
const
std
::
string
&
name
=
""
,
Function
*
parent
=
nullptr
)
:
Value
(
m
->
get_label_type
(),
parent
==
nullptr
?
GLOBAL_BASICBLOCK_NAMES_
.
get_name
(
name
)
:
parent
->
names4blocks_
.
get_name
(
name
))
,
parent_
(
parent
)
{
assert
(
parent
&&
"currently parent should not be nullptr"
);
assert
(
parent
&&
"currently parent should not be nullptr"
);
parent_
->
add_basic_block
(
this
);
parent_
->
add_basic_block
(
this
);
}
}
...
@@ -105,12 +109,9 @@ std::string BasicBlock::print() {
...
@@ -105,12 +109,9 @@ std::string BasicBlock::print() {
std
::
string
BasicBlock
::
safe_print
()
const
std
::
string
BasicBlock
::
safe_print
()
const
{
{
Function
*
parent
=
parent_
;
auto
parentName
=
((
parent
!=
nullptr
)
?
parent
->
get_name
()
:
"f<null>"
);
auto
name
=
get_name
();
auto
name
=
get_name
();
if
(
name
.
empty
())
name
=
"b"
+
ptr_to_str
(
this
);
if
(
name
.
empty
())
name
=
"b"
+
ptr_to_str
(
this
);
if
(
parentName
.
empty
())
parentName
=
"f"
+
ptr_to_str
(
parent
);
return
name
+
" "
+
std
::
to_string
(
instr_list_
.
size
())
+
"inst pre "
+
std
::
to_string
(
pre_bbs_
.
size
())
+
"b suc "
+
std
::
to_string
(
succ_bbs_
.
size
())
+
"b"
;
return
parentName
+
":"
+
name
+
" "
+
std
::
to_string
(
instr_list_
.
size
())
+
"inst pre "
+
std
::
to_string
(
pre_bbs_
.
size
())
+
"b suc "
+
std
::
to_string
(
succ_bbs_
.
size
())
+
"b"
;
}
}
BasicBlock
::~
BasicBlock
()
BasicBlock
::~
BasicBlock
()
...
@@ -123,3 +124,5 @@ BasicBlock* BasicBlock::get_entry_block_of_same_function() const
...
@@ -123,3 +124,5 @@ BasicBlock* BasicBlock::get_entry_block_of_same_function() const
assert
(
parent_
!=
nullptr
&&
"bb have no parent function"
);
assert
(
parent_
!=
nullptr
&&
"bb have no parent function"
);
return
parent_
->
get_entry_block
();
return
parent_
->
get_entry_block
();
}
}
Names
GLOBAL_BASICBLOCK_NAMES_
{
false
,
"label"
,
""
};
\ No newline at end of file
src/lightir/CMakeLists.txt
View file @
c396901a
...
@@ -10,7 +10,7 @@ add_library(
...
@@ -10,7 +10,7 @@ add_library(
Instruction.cpp
Instruction.cpp
Module.cpp
Module.cpp
IRprinter.cpp
IRprinter.cpp
)
Names.cpp
)
target_link_libraries
(
target_link_libraries
(
IR_lib
IR_lib
...
...
src/lightir/Function.cpp
View file @
c396901a
...
@@ -6,8 +6,17 @@
...
@@ -6,8 +6,17 @@
#include <unordered_set>
#include <unordered_set>
#include <queue>
#include <queue>
namespace
{
std
::
string
chopName
(
std
::
string
name
)
{
if
(
name
.
size
()
>
4
)
return
{
name
.
begin
(),
name
.
begin
()
+
4
};
return
name
;
}
}
Function
::
Function
(
FunctionType
*
ty
,
const
std
::
string
&
name
,
Module
*
parent
)
Function
::
Function
(
FunctionType
*
ty
,
const
std
::
string
&
name
,
Module
*
parent
)
:
Value
(
ty
,
name
),
parent_
(
parent
),
seq_cnt_
(
0
)
{
:
Value
(
ty
,
name
),
names4blocks_
(
false
,
"label"
,
chopName
(
name
)),
names4insts_
(
true
,
"op"
,
""
),
parent_
(
parent
),
seq_cnt_
(
0
)
{
// num_args_ = ty->getNumParams();
// num_args_ = ty->getNumParams();
parent
->
add_function
(
this
);
parent
->
add_function
(
this
);
// build args
// build args
...
...
src/lightir/Instruction.cpp
View file @
c396901a
...
@@ -10,10 +10,14 @@
...
@@ -10,10 +10,14 @@
#include <string>
#include <string>
#include <vector>
#include <vector>
Instruction
::
Instruction
(
Type
*
ty
,
OpID
id
,
BasicBlock
*
parent
)
Instruction
::
Instruction
(
Type
*
ty
,
OpID
id
,
const
std
::
string
&
name
,
BasicBlock
*
parent
)
:
User
(
ty
,
""
),
op_id_
(
id
),
parent_
(
parent
)
{
:
User
(
ty
,
""
),
op_id_
(
id
),
parent_
(
parent
)
{
assert
(
ty
!=
nullptr
&&
"Instruction have null type"
);
assert
(((
!
ty
->
is_void_type
())
||
name
.
empty
())
&&
"Void Type Instruction should not have name"
);
if
(
parent
)
if
(
parent
)
parent
->
add_instruction
(
this
);
parent
->
add_instruction
(
this
);
if
(
!
ty
->
is_void_type
()
&&
parent
&&
parent
->
get_parent
())
set_name
(
parent_
->
get_parent
()
->
names4insts_
.
get_name
(
name
));
}
}
Instruction
::~
Instruction
()
=
default
;
Instruction
::~
Instruction
()
=
default
;
...
@@ -25,50 +29,50 @@ std::string Instruction::get_instr_op_name() const {
...
@@ -25,50 +29,50 @@ std::string Instruction::get_instr_op_name() const {
return
print_instr_op_name
(
op_id_
);
return
print_instr_op_name
(
op_id_
);
}
}
IBinaryInst
::
IBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
IBinaryInst
::
IBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
bb
->
get_module
()
->
get_int32_type
(),
id
,
bb
)
{
:
Instruction
(
bb
->
get_module
()
->
get_int32_type
(),
id
,
name
,
bb
)
{
assert
(
v1
->
get_type
()
->
is_int32_type
()
&&
v2
->
get_type
()
->
is_int32_type
()
&&
assert
(
v1
->
get_type
()
->
is_int32_type
()
&&
v2
->
get_type
()
->
is_int32_type
()
&&
"IBinaryInst operands are not both i32"
);
"IBinaryInst operands are not both i32"
);
add_operand
(
v1
);
add_operand
(
v1
);
add_operand
(
v2
);
add_operand
(
v2
);
}
}
IBinaryInst
*
IBinaryInst
::
create_add
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
IBinaryInst
*
IBinaryInst
::
create_add
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
IBinaryInst
(
add
,
v1
,
v2
,
bb
);
return
new
IBinaryInst
(
add
,
v1
,
v2
,
bb
,
name
);
}
}
IBinaryInst
*
IBinaryInst
::
create_sub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
IBinaryInst
*
IBinaryInst
::
create_sub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
IBinaryInst
(
sub
,
v1
,
v2
,
bb
);
return
new
IBinaryInst
(
sub
,
v1
,
v2
,
bb
,
name
);
}
}
IBinaryInst
*
IBinaryInst
::
create_mul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
IBinaryInst
*
IBinaryInst
::
create_mul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
IBinaryInst
(
mul
,
v1
,
v2
,
bb
);
return
new
IBinaryInst
(
mul
,
v1
,
v2
,
bb
,
name
);
}
}
IBinaryInst
*
IBinaryInst
::
create_sdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
IBinaryInst
*
IBinaryInst
::
create_sdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
IBinaryInst
(
sdiv
,
v1
,
v2
,
bb
);
return
new
IBinaryInst
(
sdiv
,
v1
,
v2
,
bb
,
name
);
}
}
FBinaryInst
::
FBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
FBinaryInst
::
FBinaryInst
(
OpID
id
,
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
bb
->
get_module
()
->
get_float_type
(),
id
,
bb
)
{
:
Instruction
(
bb
->
get_module
()
->
get_float_type
(),
id
,
name
,
bb
)
{
assert
(
v1
->
get_type
()
->
is_float_type
()
&&
v2
->
get_type
()
->
is_float_type
()
&&
assert
(
v1
->
get_type
()
->
is_float_type
()
&&
v2
->
get_type
()
->
is_float_type
()
&&
"FBinaryInst operands are not both float"
);
"FBinaryInst operands are not both float"
);
add_operand
(
v1
);
add_operand
(
v1
);
add_operand
(
v2
);
add_operand
(
v2
);
}
}
FBinaryInst
*
FBinaryInst
::
create_fadd
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FBinaryInst
*
FBinaryInst
::
create_fadd
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FBinaryInst
(
fadd
,
v1
,
v2
,
bb
);
return
new
FBinaryInst
(
fadd
,
v1
,
v2
,
bb
,
name
);
}
}
FBinaryInst
*
FBinaryInst
::
create_fsub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FBinaryInst
*
FBinaryInst
::
create_fsub
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FBinaryInst
(
fsub
,
v1
,
v2
,
bb
);
return
new
FBinaryInst
(
fsub
,
v1
,
v2
,
bb
,
name
);
}
}
FBinaryInst
*
FBinaryInst
::
create_fmul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FBinaryInst
*
FBinaryInst
::
create_fmul
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FBinaryInst
(
fmul
,
v1
,
v2
,
bb
);
return
new
FBinaryInst
(
fmul
,
v1
,
v2
,
bb
,
name
);
}
}
FBinaryInst
*
FBinaryInst
::
create_fdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FBinaryInst
*
FBinaryInst
::
create_fdiv
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FBinaryInst
(
fdiv
,
v1
,
v2
,
bb
);
return
new
FBinaryInst
(
fdiv
,
v1
,
v2
,
bb
,
name
);
}
}
ICmpInst
::
ICmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
)
ICmpInst
::
ICmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
bb
->
get_module
()
->
get_int1_type
(),
id
,
bb
)
{
:
Instruction
(
bb
->
get_module
()
->
get_int1_type
(),
id
,
name
,
bb
)
{
assert
(
lhs
->
get_type
()
->
is_int32_type
()
&&
assert
(
lhs
->
get_type
()
->
is_int32_type
()
&&
rhs
->
get_type
()
->
is_int32_type
()
&&
rhs
->
get_type
()
->
is_int32_type
()
&&
"CmpInst operands are not both i32"
);
"CmpInst operands are not both i32"
);
...
@@ -76,27 +80,27 @@ ICmpInst::ICmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb)
...
@@ -76,27 +80,27 @@ ICmpInst::ICmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb)
add_operand
(
rhs
);
add_operand
(
rhs
);
}
}
ICmpInst
*
ICmpInst
::
create_ge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
ICmpInst
*
ICmpInst
::
create_ge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ICmpInst
(
ge
,
v1
,
v2
,
bb
);
return
new
ICmpInst
(
ge
,
v1
,
v2
,
bb
,
name
);
}
}
ICmpInst
*
ICmpInst
::
create_gt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
ICmpInst
*
ICmpInst
::
create_gt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ICmpInst
(
gt
,
v1
,
v2
,
bb
);
return
new
ICmpInst
(
gt
,
v1
,
v2
,
bb
,
name
);
}
}
ICmpInst
*
ICmpInst
::
create_le
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
ICmpInst
*
ICmpInst
::
create_le
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ICmpInst
(
le
,
v1
,
v2
,
bb
);
return
new
ICmpInst
(
le
,
v1
,
v2
,
bb
,
name
);
}
}
ICmpInst
*
ICmpInst
::
create_lt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
ICmpInst
*
ICmpInst
::
create_lt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ICmpInst
(
lt
,
v1
,
v2
,
bb
);
return
new
ICmpInst
(
lt
,
v1
,
v2
,
bb
,
name
);
}
}
ICmpInst
*
ICmpInst
::
create_eq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
ICmpInst
*
ICmpInst
::
create_eq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ICmpInst
(
eq
,
v1
,
v2
,
bb
);
return
new
ICmpInst
(
eq
,
v1
,
v2
,
bb
,
name
);
}
}
ICmpInst
*
ICmpInst
::
create_ne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
ICmpInst
*
ICmpInst
::
create_ne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ICmpInst
(
ne
,
v1
,
v2
,
bb
);
return
new
ICmpInst
(
ne
,
v1
,
v2
,
bb
,
name
);
}
}
FCmpInst
::
FCmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
)
FCmpInst
::
FCmpInst
(
OpID
id
,
Value
*
lhs
,
Value
*
rhs
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
bb
->
get_module
()
->
get_int1_type
(),
id
,
bb
)
{
:
Instruction
(
bb
->
get_module
()
->
get_int1_type
(),
id
,
name
,
bb
)
{
assert
(
lhs
->
get_type
()
->
is_float_type
()
&&
assert
(
lhs
->
get_type
()
->
is_float_type
()
&&
rhs
->
get_type
()
->
is_float_type
()
&&
rhs
->
get_type
()
->
is_float_type
()
&&
"FCmpInst operands are not both float"
);
"FCmpInst operands are not both float"
);
...
@@ -104,27 +108,27 @@ FCmpInst::FCmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb)
...
@@ -104,27 +108,27 @@ FCmpInst::FCmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb)
add_operand
(
rhs
);
add_operand
(
rhs
);
}
}
FCmpInst
*
FCmpInst
::
create_fge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FCmpInst
*
FCmpInst
::
create_fge
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FCmpInst
(
fge
,
v1
,
v2
,
bb
);
return
new
FCmpInst
(
fge
,
v1
,
v2
,
bb
,
name
);
}
}
FCmpInst
*
FCmpInst
::
create_fgt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FCmpInst
*
FCmpInst
::
create_fgt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FCmpInst
(
fgt
,
v1
,
v2
,
bb
);
return
new
FCmpInst
(
fgt
,
v1
,
v2
,
bb
,
name
);
}
}
FCmpInst
*
FCmpInst
::
create_fle
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FCmpInst
*
FCmpInst
::
create_fle
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FCmpInst
(
fle
,
v1
,
v2
,
bb
);
return
new
FCmpInst
(
fle
,
v1
,
v2
,
bb
,
name
);
}
}
FCmpInst
*
FCmpInst
::
create_flt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FCmpInst
*
FCmpInst
::
create_flt
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FCmpInst
(
flt
,
v1
,
v2
,
bb
);
return
new
FCmpInst
(
flt
,
v1
,
v2
,
bb
,
name
);
}
}
FCmpInst
*
FCmpInst
::
create_feq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FCmpInst
*
FCmpInst
::
create_feq
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FCmpInst
(
feq
,
v1
,
v2
,
bb
);
return
new
FCmpInst
(
feq
,
v1
,
v2
,
bb
,
name
);
}
}
FCmpInst
*
FCmpInst
::
create_fne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
)
{
FCmpInst
*
FCmpInst
::
create_fne
(
Value
*
v1
,
Value
*
v2
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FCmpInst
(
fne
,
v1
,
v2
,
bb
);
return
new
FCmpInst
(
fne
,
v1
,
v2
,
bb
,
name
);
}
}
CallInst
::
CallInst
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
BasicBlock
*
bb
)
CallInst
::
CallInst
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
func
->
get_return_type
(),
call
,
bb
)
{
:
Instruction
(
func
->
get_return_type
(),
call
,
name
,
bb
)
{
assert
(
func
->
get_type
()
->
is_function_type
()
&&
"Not a function"
);
assert
(
func
->
get_type
()
->
is_function_type
()
&&
"Not a function"
);
assert
((
func
->
get_num_of_args
()
==
args
.
size
())
&&
"Wrong number of args"
);
assert
((
func
->
get_num_of_args
()
==
args
.
size
())
&&
"Wrong number of args"
);
add_operand
(
func
);
add_operand
(
func
);
...
@@ -137,8 +141,8 @@ CallInst::CallInst(Function *func, const std::vector<Value *>& args, BasicBlock
...
@@ -137,8 +141,8 @@ CallInst::CallInst(Function *func, const std::vector<Value *>& args, BasicBlock
}
}
CallInst
*
CallInst
::
create_call
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
CallInst
*
CallInst
::
create_call
(
Function
*
func
,
const
std
::
vector
<
Value
*>&
args
,
BasicBlock
*
bb
)
{
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
CallInst
(
func
,
args
,
bb
);
return
new
CallInst
(
func
,
args
,
bb
,
name
);
}
}
FunctionType
*
CallInst
::
get_function_type
()
const
{
FunctionType
*
CallInst
::
get_function_type
()
const
{
...
@@ -147,7 +151,7 @@ FunctionType *CallInst::get_function_type() const {
...
@@ -147,7 +151,7 @@ FunctionType *CallInst::get_function_type() const {
BranchInst
::
BranchInst
(
Value
*
cond
,
BasicBlock
*
if_true
,
BasicBlock
*
if_false
,
BranchInst
::
BranchInst
(
Value
*
cond
,
BasicBlock
*
if_true
,
BasicBlock
*
if_false
,
BasicBlock
*
bb
)
BasicBlock
*
bb
)
:
Instruction
(
bb
->
get_module
()
->
get_void_type
(),
br
,
bb
)
{
:
Instruction
(
bb
->
get_module
()
->
get_void_type
(),
br
,
""
,
bb
)
{
if
(
cond
==
nullptr
)
{
// conditionless jump
if
(
cond
==
nullptr
)
{
// conditionless jump
assert
(
if_false
==
nullptr
&&
"Given false-bb on conditionless jump"
);
assert
(
if_false
==
nullptr
&&
"Given false-bb on conditionless jump"
);
add_operand
(
if_true
);
add_operand
(
if_true
);
...
@@ -194,7 +198,7 @@ BranchInst *BranchInst::create_br(BasicBlock *if_true, BasicBlock *bb) {
...
@@ -194,7 +198,7 @@ BranchInst *BranchInst::create_br(BasicBlock *if_true, BasicBlock *bb) {
}
}
ReturnInst
::
ReturnInst
(
Value
*
val
,
BasicBlock
*
bb
)
ReturnInst
::
ReturnInst
(
Value
*
val
,
BasicBlock
*
bb
)
:
Instruction
(
bb
->
get_module
()
->
get_void_type
(),
ret
,
bb
)
{
:
Instruction
(
bb
->
get_module
()
->
get_void_type
(),
ret
,
""
,
bb
)
{
if
(
val
==
nullptr
)
{
if
(
val
==
nullptr
)
{
assert
(
bb
->
get_parent
()
->
get_return_type
()
->
is_void_type
());
assert
(
bb
->
get_parent
()
->
get_return_type
()
->
is_void_type
());
}
else
{
}
else
{
...
@@ -216,9 +220,9 @@ ReturnInst *ReturnInst::create_void_ret(BasicBlock *bb) {
...
@@ -216,9 +220,9 @@ ReturnInst *ReturnInst::create_void_ret(BasicBlock *bb) {
bool
ReturnInst
::
is_void_ret
()
const
{
return
get_num_operand
()
==
0
;
}
bool
ReturnInst
::
is_void_ret
()
const
{
return
get_num_operand
()
==
0
;
}
GetElementPtrInst
::
GetElementPtrInst
(
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
GetElementPtrInst
::
GetElementPtrInst
(
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
BasicBlock
*
bb
)
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
PointerType
::
get
(
get_element_type
(
ptr
,
idxs
)),
:
Instruction
(
PointerType
::
get
(
get_element_type
(
ptr
,
idxs
)),
getelementptr
,
bb
)
{
getelementptr
,
name
,
bb
)
{
add_operand
(
ptr
);
add_operand
(
ptr
);
for
(
auto
idx
:
idxs
)
for
(
auto
idx
:
idxs
)
{
{
...
@@ -228,7 +232,7 @@ GetElementPtrInst::GetElementPtrInst(Value *ptr, const std::vector<Value *>& idx
...
@@ -228,7 +232,7 @@ GetElementPtrInst::GetElementPtrInst(Value *ptr, const std::vector<Value *>& idx
}
}
Type
*
GetElementPtrInst
::
get_element_type
(
const
Value
*
ptr
,
Type
*
GetElementPtrInst
::
get_element_type
(
const
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
)
{
const
std
::
vector
<
Value
*>&
idxs
,
const
std
::
string
&
name
)
{
assert
(
ptr
->
get_type
()
->
is_pointer_type
()
&&
assert
(
ptr
->
get_type
()
->
is_pointer_type
()
&&
"GetElementPtrInst ptr is not a pointer"
);
"GetElementPtrInst ptr is not a pointer"
);
...
@@ -257,12 +261,12 @@ Type *GetElementPtrInst::get_element_type() const {
...
@@ -257,12 +261,12 @@ Type *GetElementPtrInst::get_element_type() const {
GetElementPtrInst
*
GetElementPtrInst
::
create_gep
(
Value
*
ptr
,
GetElementPtrInst
*
GetElementPtrInst
::
create_gep
(
Value
*
ptr
,
const
std
::
vector
<
Value
*>&
idxs
,
const
std
::
vector
<
Value
*>&
idxs
,
BasicBlock
*
bb
)
{
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
GetElementPtrInst
(
ptr
,
idxs
,
bb
);
return
new
GetElementPtrInst
(
ptr
,
idxs
,
bb
,
name
);
}
}
StoreInst
::
StoreInst
(
Value
*
val
,
Value
*
ptr
,
BasicBlock
*
bb
)
StoreInst
::
StoreInst
(
Value
*
val
,
Value
*
ptr
,
BasicBlock
*
bb
)
:
Instruction
(
bb
->
get_module
()
->
get_void_type
(),
store
,
bb
)
{
:
Instruction
(
bb
->
get_module
()
->
get_void_type
(),
store
,
""
,
bb
)
{
assert
((
ptr
->
get_type
()
->
get_pointer_element_type
()
==
val
->
get_type
())
&&
assert
((
ptr
->
get_type
()
->
get_pointer_element_type
()
==
val
->
get_type
())
&&
"StoreInst ptr is not a pointer to val type"
);
"StoreInst ptr is not a pointer to val type"
);
add_operand
(
val
);
add_operand
(
val
);
...
@@ -273,8 +277,8 @@ StoreInst *StoreInst::create_store(Value *val, Value *ptr, BasicBlock *bb) {
...
@@ -273,8 +277,8 @@ StoreInst *StoreInst::create_store(Value *val, Value *ptr, BasicBlock *bb) {
return
new
StoreInst
(
val
,
ptr
,
bb
);
return
new
StoreInst
(
val
,
ptr
,
bb
);
}
}
LoadInst
::
LoadInst
(
Value
*
ptr
,
BasicBlock
*
bb
)
LoadInst
::
LoadInst
(
Value
*
ptr
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
ptr
->
get_type
()
->
get_pointer_element_type
(),
load
,
:
Instruction
(
ptr
->
get_type
()
->
get_pointer_element_type
(),
load
,
name
,
bb
)
{
bb
)
{
assert
((
get_type
()
->
is_integer_type
()
or
get_type
()
->
is_float_type
()
or
assert
((
get_type
()
->
is_integer_type
()
or
get_type
()
->
is_float_type
()
or
get_type
()
->
is_pointer_type
())
&&
get_type
()
->
is_pointer_type
())
&&
...
@@ -282,12 +286,12 @@ LoadInst::LoadInst(Value *ptr, BasicBlock *bb)
...
@@ -282,12 +286,12 @@ LoadInst::LoadInst(Value *ptr, BasicBlock *bb)
add_operand
(
ptr
);
add_operand
(
ptr
);
}
}
LoadInst
*
LoadInst
::
create_load
(
Value
*
ptr
,
BasicBlock
*
bb
)
{
LoadInst
*
LoadInst
::
create_load
(
Value
*
ptr
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
LoadInst
(
ptr
,
bb
);
return
new
LoadInst
(
ptr
,
bb
,
name
);
}
}
AllocaInst
::
AllocaInst
(
Type
*
ty
,
BasicBlock
*
bb
)
AllocaInst
::
AllocaInst
(
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
PointerType
::
get
(
ty
),
alloca
,
bb
)
{
:
Instruction
(
PointerType
::
get
(
ty
),
alloca
,
name
,
bb
)
{
static
constexpr
std
::
array
<
Type
::
TypeID
,
4
>
allowed_alloc_type
=
{
static
constexpr
std
::
array
<
Type
::
TypeID
,
4
>
allowed_alloc_type
=
{
Type
::
IntegerTyID
,
Type
::
FloatTyID
,
Type
::
ArrayTyID
,
Type
::
PointerTyID
};
Type
::
IntegerTyID
,
Type
::
FloatTyID
,
Type
::
ArrayTyID
,
Type
::
PointerTyID
};
assert
(
std
::
find
(
allowed_alloc_type
.
begin
(),
allowed_alloc_type
.
end
(),
assert
(
std
::
find
(
allowed_alloc_type
.
begin
(),
allowed_alloc_type
.
end
(),
...
@@ -295,12 +299,12 @@ AllocaInst::AllocaInst(Type *ty, BasicBlock *bb)
...
@@ -295,12 +299,12 @@ AllocaInst::AllocaInst(Type *ty, BasicBlock *bb)
"Not allowed type for alloca"
);
"Not allowed type for alloca"
);
}
}
AllocaInst
*
AllocaInst
::
create_alloca
(
Type
*
ty
,
BasicBlock
*
bb
)
{
AllocaInst
*
AllocaInst
::
create_alloca
(
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
AllocaInst
(
ty
,
bb
);
return
new
AllocaInst
(
ty
,
bb
,
name
);
}
}
ZextInst
::
ZextInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
)
ZextInst
::
ZextInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
ty
,
zext
,
bb
)
{
:
Instruction
(
ty
,
zext
,
name
,
bb
)
{
assert
(
val
->
get_type
()
->
is_integer_type
()
&&
assert
(
val
->
get_type
()
->
is_integer_type
()
&&
"ZextInst operand is not integer"
);
"ZextInst operand is not integer"
);
assert
(
ty
->
is_integer_type
()
&&
"ZextInst destination type is not integer"
);
assert
(
ty
->
is_integer_type
()
&&
"ZextInst destination type is not integer"
);
...
@@ -311,15 +315,15 @@ ZextInst::ZextInst(Value *val, Type *ty, BasicBlock *bb)
...
@@ -311,15 +315,15 @@ ZextInst::ZextInst(Value *val, Type *ty, BasicBlock *bb)
add_operand
(
val
);
add_operand
(
val
);
}
}
ZextInst
*
ZextInst
::
create_zext
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
)
{
ZextInst
*
ZextInst
::
create_zext
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ZextInst
(
val
,
ty
,
bb
);
return
new
ZextInst
(
val
,
ty
,
bb
,
name
);
}
}
ZextInst
*
ZextInst
::
create_zext_to_i32
(
Value
*
val
,
BasicBlock
*
bb
)
{
ZextInst
*
ZextInst
::
create_zext_to_i32
(
Value
*
val
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
ZextInst
(
val
,
bb
->
get_module
()
->
get_int32_type
(),
bb
);
return
new
ZextInst
(
val
,
bb
->
get_module
()
->
get_int32_type
(),
bb
,
name
);
}
}
FpToSiInst
::
FpToSiInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
)
FpToSiInst
::
FpToSiInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
ty
,
fptosi
,
bb
)
{
:
Instruction
(
ty
,
fptosi
,
name
,
bb
)
{
assert
(
val
->
get_type
()
->
is_float_type
()
&&
assert
(
val
->
get_type
()
->
is_float_type
()
&&
"FpToSiInst operand is not float"
);
"FpToSiInst operand is not float"
);
assert
(
ty
->
is_integer_type
()
&&
assert
(
ty
->
is_integer_type
()
&&
...
@@ -327,28 +331,28 @@ FpToSiInst::FpToSiInst(Value *val, Type *ty, BasicBlock *bb)
...
@@ -327,28 +331,28 @@ FpToSiInst::FpToSiInst(Value *val, Type *ty, BasicBlock *bb)
add_operand
(
val
);
add_operand
(
val
);
}
}
FpToSiInst
*
FpToSiInst
::
create_fptosi
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
)
{
FpToSiInst
*
FpToSiInst
::
create_fptosi
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FpToSiInst
(
val
,
ty
,
bb
);
return
new
FpToSiInst
(
val
,
ty
,
bb
,
name
);
}
}
FpToSiInst
*
FpToSiInst
::
create_fptosi_to_i32
(
Value
*
val
,
BasicBlock
*
bb
)
{
FpToSiInst
*
FpToSiInst
::
create_fptosi_to_i32
(
Value
*
val
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
FpToSiInst
(
val
,
bb
->
get_module
()
->
get_int32_type
(),
bb
);
return
new
FpToSiInst
(
val
,
bb
->
get_module
()
->
get_int32_type
(),
bb
,
name
);
}
}
SiToFpInst
::
SiToFpInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
)
SiToFpInst
::
SiToFpInst
(
Value
*
val
,
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
ty
,
sitofp
,
bb
)
{
:
Instruction
(
ty
,
sitofp
,
name
,
bb
)
{
assert
(
val
->
get_type
()
->
is_integer_type
()
&&
assert
(
val
->
get_type
()
->
is_integer_type
()
&&
"SiToFpInst operand is not integer"
);
"SiToFpInst operand is not integer"
);
assert
(
ty
->
is_float_type
()
&&
"SiToFpInst destination type is not float"
);
assert
(
ty
->
is_float_type
()
&&
"SiToFpInst destination type is not float"
);
add_operand
(
val
);
add_operand
(
val
);
}
}
SiToFpInst
*
SiToFpInst
::
create_sitofp
(
Value
*
val
,
BasicBlock
*
bb
)
{
SiToFpInst
*
SiToFpInst
::
create_sitofp
(
Value
*
val
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
{
return
new
SiToFpInst
(
val
,
bb
->
get_module
()
->
get_float_type
(),
bb
);
return
new
SiToFpInst
(
val
,
bb
->
get_module
()
->
get_float_type
(),
bb
,
name
);
}
}
PhiInst
::
PhiInst
(
Type
*
ty
,
const
std
::
vector
<
Value
*>&
vals
,
PhiInst
::
PhiInst
(
Type
*
ty
,
const
std
::
vector
<
Value
*>&
vals
,
const
std
::
vector
<
BasicBlock
*>&
val_bbs
,
BasicBlock
*
bb
)
const
std
::
vector
<
BasicBlock
*>&
val_bbs
,
BasicBlock
*
bb
,
const
std
::
string
&
name
)
:
Instruction
(
ty
,
phi
)
{
:
Instruction
(
ty
,
phi
,
name
)
{
assert
(
vals
.
size
()
==
val_bbs
.
size
()
&&
"Unmatched vals and bbs"
);
assert
(
vals
.
size
()
==
val_bbs
.
size
()
&&
"Unmatched vals and bbs"
);
for
(
unsigned
i
=
0
;
i
<
vals
.
size
();
i
++
)
{
for
(
unsigned
i
=
0
;
i
<
vals
.
size
();
i
++
)
{
assert
(
ty
==
vals
[
i
]
->
get_type
()
&&
"Bad type for phi"
);
assert
(
ty
==
vals
[
i
]
->
get_type
()
&&
"Bad type for phi"
);
...
@@ -360,6 +364,8 @@ PhiInst::PhiInst(Type *ty, const std::vector<Value *>& vals,
...
@@ -360,6 +364,8 @@ PhiInst::PhiInst(Type *ty, const std::vector<Value *>& vals,
PhiInst
*
PhiInst
::
create_phi
(
Type
*
ty
,
BasicBlock
*
bb
,
PhiInst
*
PhiInst
::
create_phi
(
Type
*
ty
,
BasicBlock
*
bb
,
const
std
::
vector
<
Value
*>&
vals
,
const
std
::
vector
<
Value
*>&
vals
,
const
std
::
vector
<
BasicBlock
*>&
val_bbs
)
{
const
std
::
vector
<
BasicBlock
*>&
val_bbs
,
const
std
::
string
&
name
)
{
return
new
PhiInst
(
ty
,
vals
,
val_bbs
,
bb
);
return
new
PhiInst
(
ty
,
vals
,
val_bbs
,
bb
,
name
);
}
}
Names
GLOBAL_INSTRUCTION_NAMES_
{
true
,
"op"
,
""
};
\ No newline at end of file
src/lightir/Names.cpp
0 → 100644
View file @
c396901a
#include "Names.hpp"
std
::
string
Names
::
get_name
(
std
::
string
name
)
{
int
ed
=
static_cast
<
int
>
(
name
.
size
());
while
(
ed
>
0
)
{
char
ch
=
name
[
ed
-
1
];
if
((
use_underline_
&&
ch
==
'_'
)
||
(
ch
>=
'0'
&&
ch
<=
'9'
))
ed
--
;
else
break
;
}
if
(
ed
==
0
)
return
get_name
();
std
::
string
name1
=
{
name
.
begin
(),
name
.
begin
()
+
ed
};
if
(
name1
==
default_prefix_
)
return
get_name
();
auto
get
=
appended_prefix_
+
name1
;
auto
idx
=
allocated_
[
name1
]
++
;
if
(
idx
==
0
)
return
get
;
if
(
use_underline_
)
get
+=
"_"
;
return
get
+
std
::
to_string
(
idx
);
}
std
::
string
Names
::
get_name
()
{
auto
get
=
appended_prefix_
+
default_prefix_
;
return
get
+
std
::
to_string
(
++
default_prefix_used_count_
);
}
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