Skip to content

Commit 53bd231

Browse files
committed
Compleate revamp of constant data handling
1 parent a6b07f9 commit 53bd231

File tree

16 files changed

+249
-197
lines changed

16 files changed

+249
-197
lines changed

cilly/src/cil_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a> Iterator for CILIter<'a> {
149149
| CILNode::LDFtn(_)
150150
| CILNode::LDTypeToken(_)
151151
| CILNode::LocAllocAligned { tpe: _, align: _ }
152-
| CILNode::LoadGlobalAllocPtr { alloc_id: _ }
152+
| CILNode::LoadGlobalAllocPtr { .. }
153153
| CILNode::LoadAddresOfTMPLocal
154154
| CILNode::PointerToConstValue(_)
155155
| CILNode::LoadTMPLocal

cilly/src/cil_iter_mut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a> Iterator for CILIterMut<'a> {
171171
| CILNode::LoadAddresOfTMPLocal
172172
| CILNode::LoadTMPLocal
173173
| CILNode::LocAllocAligned { tpe: _, align: _ }
174-
| CILNode::LoadGlobalAllocPtr { alloc_id: _ }
174+
| CILNode::LoadGlobalAllocPtr { .. }
175175
| CILNode::PointerToConstValue(_)
176176
| CILNode::GetException
177177
| CILNode::V2(_) => {

cilly/src/cil_node.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::cilnode::MethodKind;
44
use crate::v2::method::LocalDef;
55

66
use crate::FieldDesc;
7+
use crate::IntoAsmIndex;
78
use crate::{
89
call,
910
cil_root::CILRoot,
@@ -137,6 +138,7 @@ pub enum CILNode {
137138
LdcF32(HashableF32),
138139
LoadGlobalAllocPtr {
139140
alloc_id: u64,
141+
tpe: Interned<Type>,
140142
},
141143
ConvU8(Box<Self>),
142144
ConvU16(Box<Self>),
@@ -225,6 +227,15 @@ pub enum CILNode {
225227
}
226228

227229
impl CILNode {
230+
pub fn global_alloc_ptr(
231+
alloc_id: u64,
232+
tpe: impl IntoAsmIndex<Interned<Type>>,
233+
asm: &mut Assembly,
234+
) -> Self {
235+
let tmp = tpe.into_idx(asm);
236+
//assert!(!asm[tmp].is_ptr(), "{}", asm[tmp].mangle(asm));
237+
Self::LoadGlobalAllocPtr { alloc_id, tpe: tmp }
238+
}
228239
pub fn stack_addr(val: Self, tpe_idx: Interned<Type>, _asm: &mut Assembly) -> Self {
229240
CILNode::TemporaryLocal(Box::new((
230241
tpe_idx,
@@ -459,7 +470,7 @@ impl CILNode {
459470
Self::LocAllocAligned {..}=>(),
460471
Self::CastPtr { val, new_ptr: _ }=>val.allocate_tmps(curr_loc, locals),
461472
Self:: PointerToConstValue(_arr)=>(),
462-
Self::LoadGlobalAllocPtr { alloc_id: _ } => (),
473+
Self::LoadGlobalAllocPtr { alloc_id: _,tpe:_ } => (),
463474
Self::LDLoc(_) |
464475
Self::LDArg(_) |
465476
Self::LDLocA(_)|

cilly/src/v2/asm.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,10 @@ impl Assembly {
14491449
pub(crate) fn alloc_const_data(&mut self, data: &[u8]) -> Interned<Box<[u8]>> {
14501450
self.const_data.alloc(data.into())
14511451
}
1452+
1453+
pub(crate) fn char_is_u8(&self) -> bool {
1454+
true
1455+
}
14521456
}
14531457
config!(GUARANTED_ALIGN, u8, 8);
14541458
config!(MAX_STATIC_SIZE, usize, 16);

cilly/src/v2/c_exporter/mod.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1296,12 +1296,23 @@ impl CExporter {
12961296
let mut delayed_defs: FxHashSet<ClassDefIdx> = asm.iter_class_def_ids().cloned().collect();
12971297
let mut delayed_defs_copy: FxHashSet<ClassDefIdx> = FxHashSet::default();
12981298
for (const_data, idx) in asm.const_data.1.iter() {
1299-
let data: String = const_data
1300-
.iter()
1301-
.map(|u| format!("{u}"))
1302-
.intersperse(",".into())
1303-
.collect();
1299+
let data: String = match str::from_utf8(const_data) {
1300+
Ok(s)
1301+
if asm.char_is_u8()
1302+
&& s.chars().all(|c| {
1303+
c.is_ascii_graphic() || matches!(c, '\0' | ' ' | '\n' | '\t' | '\r')
1304+
}) =>
1305+
{
1306+
format!("{s:?}")
1307+
}
1308+
Ok(_) | Err(_) => const_data
1309+
.iter()
1310+
.map(|u| format!("{u}"))
1311+
.intersperse(",".into())
1312+
.collect(),
1313+
};
13041314
let encoded = encode(idx.inner() as u64);
1315+
13051316
writeln!(type_defs, "uint8_t c_{encoded}[] = {{{data}}};")?;
13061317
}
13071318
// Ensure RustVoid present

cilly/src/v2/tpe/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,12 @@ impl Type {
287287
None
288288
}
289289
}
290+
291+
/// Returns `true` if the type is [`Ptr`].
292+
///
293+
/// [`Ptr`]: Type::Ptr
294+
#[must_use]
295+
pub fn is_ptr(&self) -> bool {
296+
matches!(self, Self::Ptr(..))
297+
}
290298
}

rustc_codegen_clr_ctx/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx, 'asm> MethodCompileCtx<'tcx, 'asm> {
6565
}
6666
}
6767
pub fn span(&self) -> Span {
68-
self.span.unwrap()
68+
self.span.unwrap_or(Span::default())
6969
}
7070
pub fn tcx_and_asm(&mut self) -> (TyCtxt<'tcx>, &mut Assembly) {
7171
(self.tcx, self.asm)

rustc_codegen_clr_type/src/type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,14 @@ pub fn get_type<'tcx>(ty: Ty<'tcx>, ctx: &mut MethodCompileCtx<'tcx, '_>) -> Typ
327327
}
328328
}
329329
//
330-
fn fixed_array(
330+
pub fn fixed_array(
331331
asm: &mut Assembly,
332332
element: Type,
333333
length: u64,
334334
arr_size: u64,
335335
align: u64,
336336
) -> Interned<ClassRef> {
337+
assert_ne!(arr_size, 0);
337338
// Get the reference to the array class
338339
let cref = ClassRef::fixed_array(element, length, asm);
339340

0 commit comments

Comments
 (0)