parent
2c8a64c763
commit
e104a3d546
@ -1,56 +0,0 @@
|
||||
use super::context::Context;
|
||||
use super::typ::Typ;
|
||||
use super::value::ConstantValue;
|
||||
use crate::utils::storage::{Arena, ArenaPtr, GenericPtr};
|
||||
|
||||
pub struct GlobalData {
|
||||
pub self_ptr: Global,
|
||||
name: String,
|
||||
value: ConstantValue,
|
||||
}
|
||||
|
||||
impl GlobalData {
|
||||
pub fn get_self_ptr(&self) -> Global {
|
||||
self.self_ptr
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Copy)]
|
||||
pub struct Global(pub GenericPtr<GlobalData>);
|
||||
|
||||
impl ArenaPtr for Global {
|
||||
type Arena = Context;
|
||||
type Data = GlobalData;
|
||||
}
|
||||
|
||||
impl Global {
|
||||
/// 创建一个全局变量
|
||||
pub fn new(ctx: &mut Context, name: String, value: ConstantValue) -> Self {
|
||||
ctx.alloc_with(|self_ptr| GlobalData {
|
||||
self_ptr,
|
||||
name,
|
||||
value,
|
||||
})
|
||||
}
|
||||
|
||||
/// 获取全局变量的名称
|
||||
pub fn name(self, ctx: &Context) -> &str {
|
||||
&self
|
||||
.deref(ctx)
|
||||
.expect("Failed to deref `name` of struct Global")
|
||||
.name
|
||||
}
|
||||
|
||||
/// 获取全局变量的值
|
||||
pub fn value(self, ctx: &Context) -> &ConstantValue {
|
||||
&self
|
||||
.deref(ctx)
|
||||
.expect("Failed to deref `value` of struct Global")
|
||||
.value
|
||||
}
|
||||
|
||||
/// 获取全局变量的类型
|
||||
pub fn typ(self, ctx: &Context) -> Typ {
|
||||
self.value(ctx).typ()
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@ -0,0 +1,16 @@
|
||||
define i32 @main() {
|
||||
bb_0:
|
||||
%v3 = alloca i32
|
||||
%v1 = alloca i32
|
||||
%v0 = alloca i32
|
||||
store i32 1, i32* %v1
|
||||
store i32 2, i32* %v3
|
||||
%v5 = load i32, i32* %v1
|
||||
%v6 = load i32, i32* %v3
|
||||
%v7 = add i32 %v5, %v6
|
||||
store i32 %v7, i32* %v0
|
||||
br label %bb_1
|
||||
bb_1:
|
||||
%v8 = load i32, i32* %v0
|
||||
ret i32 %v8
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
.arch armv8-a
|
||||
.text
|
||||
.global main
|
||||
.type main, %function
|
||||
.p2align 3
|
||||
main:
|
||||
sub sp, sp, #16
|
||||
movz w13, #1
|
||||
str w13, [sp, #4]
|
||||
movz w11, #2
|
||||
str w11, [sp]
|
||||
ldr w12, [sp, #4]
|
||||
ldr w10, [sp]
|
||||
add w14, w12, w10
|
||||
str w14, [sp, #8]
|
||||
b bb_1
|
||||
bb_1:
|
||||
ldr w9, [sp, #8]
|
||||
mov w0, w9
|
||||
add sp, sp, #16
|
||||
ret
|
||||
|
||||
|
||||
@ -0,0 +1,97 @@
|
||||
CompUnit {
|
||||
items: [
|
||||
FuncDef(
|
||||
FuncDef {
|
||||
typ: Int,
|
||||
id: "main",
|
||||
params: [],
|
||||
block: Block {
|
||||
items: [
|
||||
Decl(
|
||||
VarDecl(
|
||||
VarDecl {
|
||||
typ: Int,
|
||||
defs: [
|
||||
VarDef {
|
||||
id: "a",
|
||||
init: Some(
|
||||
Exp(
|
||||
Exp {
|
||||
kind: Const(
|
||||
Int(
|
||||
1,
|
||||
),
|
||||
),
|
||||
typ: Some(
|
||||
Int,
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
),
|
||||
),
|
||||
Decl(
|
||||
VarDecl(
|
||||
VarDecl {
|
||||
typ: Int,
|
||||
defs: [
|
||||
VarDef {
|
||||
id: "b",
|
||||
init: Some(
|
||||
Exp(
|
||||
Exp {
|
||||
kind: Const(
|
||||
Int(
|
||||
2,
|
||||
),
|
||||
),
|
||||
typ: Some(
|
||||
Int,
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
),
|
||||
),
|
||||
Stmt(
|
||||
Return(
|
||||
Return {
|
||||
exp: Some(
|
||||
Exp {
|
||||
kind: Binary(
|
||||
Add,
|
||||
Exp {
|
||||
kind: LVal(
|
||||
LVal {
|
||||
id: "a",
|
||||
},
|
||||
),
|
||||
typ: None,
|
||||
},
|
||||
Exp {
|
||||
kind: LVal(
|
||||
LVal {
|
||||
id: "b",
|
||||
},
|
||||
),
|
||||
typ: None,
|
||||
},
|
||||
),
|
||||
typ: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
},
|
||||
},
|
||||
),
|
||||
],
|
||||
}
|
||||
Loading…
Reference in new issue