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
Expand all
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 {
// 用于 lldb 调试生成 summary
std
::
string
safe_print
()
const
;
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
*>
succ_bbs_
;
std
::
list
<
Instruction
*>
instr_list_
;
Function
*
parent_
;
};
extern
Names
GLOBAL_BASICBLOCK_NAMES_
;
\ No newline at end of file
include/lightir/Function.hpp
View file @
c396901a
...
...
@@ -7,6 +7,8 @@
#include <list>
#include <memory>
#include "Names.hpp"
class
Module
;
class
Argument
;
class
Type
;
...
...
@@ -49,6 +51,11 @@ class Function : public Value {
// 用于 lldb 调试生成 summary
std
::
string
safe_print
()
const
;
// 用于给 basicblock 分配名称
Names
names4blocks_
;
Names
names4insts_
;
private:
std
::
list
<
BasicBlock
*>
basic_blocks_
;
std
::
list
<
Argument
>
arguments_
;
...
...
include/lightir/IRBuilder.hpp
View file @
c396901a
...
...
@@ -105,9 +105,9 @@ class IRBuilder {
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
...
...
include/lightir/Instruction.hpp
View file @
c396901a
This diff is collapsed.
Click to expand it.
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) {
auto
nowBB
=
builder
->
get_insert_block
();
auto
entryBB
=
context
.
func
->
get_entry_block
();
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
);
scope
.
push
(
node
.
id
,
var
);
}
else
{
...
...
@@ -99,7 +99,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) {
auto
entryBB
=
context
.
func
->
get_entry_block
();
builder
->
set_insert_point
(
entryBB
);
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
);
scope
.
push
(
node
.
id
,
var
);
}
...
...
@@ -149,18 +149,18 @@ Value* CminusfBuilder::visit(ASTFunDeclaration &node) {
if
(
node
.
params
[
i
]
->
isarray
)
{
Value
*
array_alloc
;
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
{
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
);
scope
.
push
(
node
.
params
[
i
]
->
id
,
array_alloc
);
}
else
{
Value
*
alloc
;
if
(
node
.
params
[
i
]
->
type
==
TYPE_INT
)
{
alloc
=
builder
->
create_alloca
(
INT32_T
);
alloc
=
builder
->
create_alloca
(
INT32_T
,
node
.
params
[
i
]
->
id
);
}
else
{
alloc
=
builder
->
create_alloca
(
FLOAT_T
);
alloc
=
builder
->
create_alloca
(
FLOAT_T
,
node
.
params
[
i
]
->
id
);
}
builder
->
create_store
(
args
[
i
],
alloc
);
scope
.
push
(
node
.
params
[
i
]
->
id
,
alloc
);
...
...
src/lightir/BasicBlock.cpp
View file @
c396901a
...
...
@@ -7,9 +7,13 @@
#include <cassert>
BasicBlock
::
BasicBlock
(
Module
*
m
,
const
std
::
string
&
name
=
""
,
Function
*
parent
=
nullptr
)
:
Value
(
m
->
get_label_type
(),
name
),
parent_
(
parent
)
{
#include "Names.hpp"
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"
);
parent_
->
add_basic_block
(
this
);
}
...
...
@@ -105,12 +109,9 @@ std::string BasicBlock::print() {
std
::
string
BasicBlock
::
safe_print
()
const
{
Function
*
parent
=
parent_
;
auto
parentName
=
((
parent
!=
nullptr
)
?
parent
->
get_name
()
:
"f<null>"
);
auto
name
=
get_name
();
if
(
name
.
empty
())
name
=
"b"
+
ptr_to_str
(
this
);
if
(
parentName
.
empty
())
parentName
=
"f"
+
ptr_to_str
(
parent
);
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"
;
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"
;
}
BasicBlock
::~
BasicBlock
()
...
...
@@ -123,3 +124,5 @@ BasicBlock* BasicBlock::get_entry_block_of_same_function() const
assert
(
parent_
!=
nullptr
&&
"bb have no parent function"
);
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(
Instruction.cpp
Module.cpp
IRprinter.cpp
)
Names.cpp
)
target_link_libraries
(
IR_lib
...
...
src/lightir/Function.cpp
View file @
c396901a
...
...
@@ -6,8 +6,17 @@
#include <unordered_set>
#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
)
:
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();
parent
->
add_function
(
this
);
// build args
...
...
src/lightir/Instruction.cpp
View file @
c396901a
This diff is collapsed.
Click to expand it.
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