Skip to content

Commit 43d1b2e

Browse files
committed
Better constant optimization
1 parent 63542da commit 43d1b2e

File tree

13 files changed

+341
-131
lines changed

13 files changed

+341
-131
lines changed

cilly/src/bin/linker/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ fn add_mandatory_statics(asm: &mut cilly::Assembly) {
3232
"__rust_alloc_error_handler_should_panic",
3333
false,
3434
main_module,
35+
None,
36+
false,
3537
);
3638
asm.add_static(
3739
cilly::Type::Int(cilly::Int::U8),
3840
"__rust_no_alloc_shim_is_unstable",
3941
false,
4042
main_module,
43+
None,
44+
false,
4145
);
4246
}
4347
static FORCE_FAIL: std::sync::LazyLock<bool> =

cilly/src/utilis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub fn get_environ(asm: &mut Assembly) -> Interned<MethodRef> {
327327

328328
let def = MethodDef::from_v1(&get_environ, asm, main_module);
329329
asm.new_method(def);
330-
asm.add_static(uint8_ptr_ptr, "environ", true, main_module);
330+
asm.add_static(uint8_ptr_ptr, "environ", true, main_module, None, false);
331331
init_cs
332332
}
333333
static CHARS: &[char] = &[

cilly/src/v2/asm.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ impl Assembly {
455455
name: impl Into<IString>,
456456
thread_local: bool,
457457
in_class: ClassDefIdx,
458+
default_value: Option<Const>,
459+
is_const: bool,
458460
) -> Interned<StaticFieldDesc> {
459461
let name = self.alloc_string(name);
460462
let sfld = StaticFieldDesc::new(*in_class, name, tpe);
@@ -466,6 +468,8 @@ impl Assembly {
466468
tpe,
467469
name,
468470
is_tls: thread_local,
471+
default_value,
472+
is_const,
469473
})
470474
{
471475
self.class_mut(in_class)
@@ -474,6 +478,8 @@ impl Assembly {
474478
tpe,
475479
name,
476480
is_tls: thread_local,
481+
default_value,
482+
is_const,
477483
});
478484
}
479485

@@ -1434,7 +1440,7 @@ impl Assembly {
14341440

14351441
pub(crate) fn global_void(&mut self) -> Interned<StaticFieldDesc> {
14361442
let main = self.main_module();
1437-
self.add_static(Type::Void, "global_void", false, main)
1443+
self.add_static(Type::Void, "global_void", false, main, None, true)
14381444
}
14391445

14401446
pub(crate) fn alloc_const_data(&mut self, data: &[u8]) -> Interned<Box<[u8]>> {

cilly/src/v2/asm_link.rs

+38-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{
22
asm::{CCTOR, TCCTOR, USER_INIT},
33
bimap::Interned,
44
class::{ClassDefIdx, StaticFieldDef},
5-
Assembly, BasicBlock, CILNode, CILRoot, ClassDef, ClassRef, FieldDesc, FnSig, MethodDef,
5+
Assembly, BasicBlock, CILNode, CILRoot, ClassDef, ClassRef, Const, FieldDesc, FnSig, MethodDef,
66
MethodDefIdx, MethodRef, StaticFieldDesc, Type,
77
};
88
impl Assembly {
@@ -99,27 +99,29 @@ impl Assembly {
9999
.collect();
100100
MethodRef::new(class, name, sig, method_ref.kind(), generics)
101101
}
102+
pub(crate) fn translate_const(&mut self, source: &Assembly, cst: &Const) -> Const {
103+
match cst {
104+
super::Const::PlatformString(pstr) => {
105+
super::Const::PlatformString(self.alloc_string(source[*pstr].as_ref()))
106+
}
107+
108+
super::Const::Null(cref) => super::Const::Null(self.translate_class_ref(source, *cref)),
109+
super::Const::ByteBuffer { data, tpe } => {
110+
let tpe = self.translate_type(source, source[*tpe]);
111+
(super::Const::ByteBuffer {
112+
data: self.alloc_const_data(&source.const_data[*data]),
113+
tpe: self.alloc_type(tpe),
114+
})
115+
}
116+
_ => cst.clone(),
117+
}
118+
}
102119
// The complexity of this function is unavoidable.
103120
#[allow(clippy::too_many_lines)]
104121
pub(crate) fn translate_node(&mut self, source: &Assembly, node: CILNode) -> CILNode {
105122
match &node {
106123
CILNode::LdLoc(_) | CILNode::LdLocA(_) | CILNode::LdArg(_) | CILNode::LdArgA(_) => node,
107-
CILNode::Const(cst) => match cst.as_ref() {
108-
super::Const::PlatformString(pstr) => CILNode::Const(Box::new(
109-
super::Const::PlatformString(self.alloc_string(source[*pstr].as_ref())),
110-
)),
111-
super::Const::Null(cref) => CILNode::Const(Box::new(super::Const::Null(
112-
self.translate_class_ref(source, *cref),
113-
))),
114-
super::Const::ByteBuffer { data, tpe } => {
115-
let tpe = self.translate_type(source, source[*tpe]);
116-
CILNode::Const(Box::new(super::Const::ByteBuffer {
117-
data: self.alloc_const_data(&source.const_data[*data]),
118-
tpe: self.alloc_type(tpe),
119-
}))
120-
}
121-
_ => node.clone(),
122-
},
124+
CILNode::Const(cst) => CILNode::Const(Box::new(self.translate_const(source, cst))),
123125
CILNode::BinOp(a, b, op) => {
124126
let a = self.translate_node(source, source.get_node(*a).clone());
125127
let b = self.translate_node(source, source.get_node(*b).clone());
@@ -608,15 +610,25 @@ impl Assembly {
608610
let static_fields = def
609611
.static_fields()
610612
.iter()
611-
.map(|StaticFieldDef { tpe, name, is_tls }| {
612-
let tpe = self.translate_type(source, *tpe);
613-
let name = self.alloc_string(source[*name].as_ref());
614-
StaticFieldDef {
615-
tpe,
616-
name,
617-
is_tls: *is_tls,
618-
}
619-
})
613+
.map(
614+
|StaticFieldDef {
615+
tpe,
616+
name,
617+
is_tls,
618+
default_value,
619+
is_const,
620+
}| {
621+
let tpe = self.translate_type(source, *tpe);
622+
let name = self.alloc_string(source[*name].as_ref());
623+
StaticFieldDef {
624+
tpe,
625+
name,
626+
is_tls: *is_tls,
627+
default_value: default_value.map(|cst| self.translate_const(source, &cst)),
628+
is_const: *is_const,
629+
}
630+
},
631+
)
620632
.collect();
621633
let translated = ClassDef::new(
622634
name,

cilly/src/v2/builtins/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -919,10 +919,17 @@ pub fn argc_argv_init(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
919919
drop(blocks);
920920
let def = MethodDef::from_v1(&init_method, asm, main_module);
921921
asm.new_method(def);
922-
asm.add_static(Type::Bool, "argv_argc_init_status", false, main_module);
922+
asm.add_static(
923+
Type::Bool,
924+
"argv_argc_init_status",
925+
false,
926+
main_module,
927+
None,
928+
false,
929+
);
923930
let uint8_ptr_ptr = asm.nptr(uint8_ptr);
924-
asm.add_static(uint8_ptr_ptr, "argv", false, main_module);
925-
asm.add_static(Type::Int(Int::I32), "argc", false, main_module);
931+
asm.add_static(uint8_ptr_ptr, "argv", false, main_module, None, false);
932+
asm.add_static(Type::Int(Int::I32), "argc", false, main_module, None, false);
926933
MethodDef::from_v1(&init_method, asm, main_module)
927934
.implementation()
928935
.clone()

cilly/src/v2/builtins/thread.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,22 @@ pub fn instert_threading(asm: &mut Assembly, patcher: &mut MissingMethodPatcher)
372372
insert_pthread_setname_np(asm, patcher);
373373
insert_pthread_key_delete(asm, patcher);
374374
let main_mod = asm.main_module();
375-
asm.add_static(Type::Int(PTHREAD_KEY_T), "last_val", false, main_mod);
375+
asm.add_static(
376+
Type::Int(PTHREAD_KEY_T),
377+
"last_val",
378+
false,
379+
main_mod,
380+
None,
381+
false,
382+
);
376383
let thread_key_dict = thread_key_dict(asm);
377384
asm.add_static(
378385
Type::ClassRef(thread_key_dict),
379386
"pthread_keys",
380387
true,
381388
main_mod,
389+
None,
390+
false,
382391
);
383392
let pthread_keys = asm.alloc_string("pthread_keys");
384393
let pthread_keys_static = asm.alloc_sfld(StaticFieldDesc::new(
@@ -497,7 +506,14 @@ pub fn instert_threading(asm: &mut Assembly, patcher: &mut MissingMethodPatcher)
497506
let main_module = asm.main_module();
498507
let thread_results = asm.alloc_string("thread_results");
499508
let dict = ClassRef::concurent_dictionary(Type::Int(Int::I32), Type::Int(Int::ISize), asm);
500-
asm.add_static(Type::ClassRef(dict), "thread_results", false, main_module);
509+
asm.add_static(
510+
Type::ClassRef(dict),
511+
"thread_results",
512+
false,
513+
main_module,
514+
None,
515+
false,
516+
);
501517
let thread_results = asm.alloc_sfld(StaticFieldDesc::new(
502518
*main_module,
503519
thread_results,

0 commit comments

Comments
 (0)