1use std::cmp::Ordering;
29use std::collections::{BTreeMap, HashMap, HashSet};
30
31use rucc_base::{Interner, Symbol};
32use rucc_diag::{Diagnostic, Span};
33use rucc_ir::{
34 DataList, Datum, Func, Global, Imm, Linkage as IrLinkage, Module, Reloc, TlsModel, Type,
35};
36use rucc_sema::{
37 Base, Const, DeclId, DeclKind, Definition, Eval, ExprId, ExprKind, InitEntry, InitList,
38 Linkage, StorageDuration, StrId, Tast,
39};
40use rucc_target::TargetInfo;
41use rucc_types::{TypeId, TypeKind, Types};
42
43use crate::abi::{self, Plan};
44use crate::body;
45use crate::repr;
46
47#[derive(Debug)]
52pub struct Context<'a> {
53 pub tast: &'a Tast,
55 pub types: &'a Types,
57 pub target: &'a TargetInfo,
59 pub names: &'a mut Interner,
61}
62
63#[derive(Debug)]
65pub struct Lowered {
66 pub module: Module,
69 pub diagnostics: Vec<Diagnostic>,
71}
72
73#[must_use]
77pub fn lower(name: &str, cx: Context<'_>) -> Lowered {
78 let Context { tast, types, target, names } = cx;
79 let module = Module::new(names.intern(name), target);
80 let mut unit = Unit {
81 tast,
82 types,
83 target,
84 names,
85 module,
86 diagnostics: Vec::new(),
87 strings: HashMap::new(),
88 statics: HashMap::new(),
89 done: HashSet::new(),
90 };
91 unit.run();
92 Lowered { module: unit.module, diagnostics: unit.diagnostics }
93}
94
95pub(crate) struct Unit<'a> {
97 pub(crate) tast: &'a Tast,
98 pub(crate) types: &'a Types,
99 pub(crate) target: &'a TargetInfo,
100 pub(crate) names: &'a mut Interner,
101 pub(crate) module: Module,
102 pub(crate) diagnostics: Vec<Diagnostic>,
103 strings: HashMap<StrId, Symbol>,
106 statics: HashMap<DeclId, Symbol>,
108 done: HashSet<DeclId>,
110}
111
112impl std::fmt::Debug for Unit<'_> {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 f.debug_struct("Unit")
117 .field("module", &self.module.counts())
118 .field("diagnostics", &self.diagnostics.len())
119 .finish()
120 }
121}
122
123impl Unit<'_> {
124 fn run(&mut self) {
126 for index in 0..self.tast.top_level().len() {
127 let decl = self.tast.top_level()[index];
128 if !self.done.insert(decl) {
129 continue;
130 }
131 match self.tast[decl].kind {
132 DeclKind::Function => self.function(decl),
133 DeclKind::Object => self.object(decl),
134 }
135 }
136 }
137
138 fn object(&mut self, decl: DeclId) {
140 let tast = self.tast;
141 let node = &tast[decl];
142 let (ty, state, init) = (node.ty, node.state, node.init);
143 let (linkage, duration, alignment) = (node.linkage, node.duration, node.alignment);
144 let span = tast.decl_span(decl);
145 if duration == StorageDuration::Automatic {
146 return;
149 }
150
151 let symbol = self.symbol_of(decl);
152 let size = repr::size_of(self.types, self.target, ty);
153 let align = alignment.unwrap_or_else(|| repr::align_of(self.types, self.target, ty));
154 let mut global = Global::new(symbol, size, align);
155 global.linkage = match linkage {
156 Linkage::External => IrLinkage::External,
157 Linkage::Internal | Linkage::None => IrLinkage::Internal,
158 };
159 global.tls = (duration == StorageDuration::Thread).then_some(TlsModel::GlobalDynamic);
160 global.constant = repr::is_read_only(self.types, ty);
161 global.init = match state {
162 Definition::Declared => None,
166 Definition::Tentative => Some(self.zeros(size)),
167 Definition::Defined => Some(self.image(init, size, span)),
168 };
169 self.module.add_global(global);
170 }
171
172 fn function(&mut self, decl: DeclId) {
174 let tast = self.tast;
175 let node = &tast[decl];
176 let (ty, linkage, body) = (node.ty, node.linkage, node.body);
177 let span = tast.decl_span(decl);
178 let Some(name) = node.name else { return };
179 let Some(plan) = self.plan(ty, &[], span) else { return };
180
181 let mut func = Func::new(name, plan.signature.clone());
182 func.linkage = match linkage {
183 Linkage::Internal | Linkage::None => IrLinkage::Internal,
184 Linkage::External => IrLinkage::External,
185 };
186 if body.is_some() {
187 body::lower(self, decl, &mut func, &plan);
188 }
189 self.module.add_func(func);
190 }
191
192 pub(crate) fn plan(&mut self, ty: TypeId, actual: &[TypeId], span: Span) -> Option<Plan> {
199 let canonical = self.types.canonical(ty);
200 let canonical = match self.types.kind(canonical) {
201 TypeKind::Pointer(pointee) => self.types.canonical(pointee),
203 _ => canonical,
204 };
205 let TypeKind::Function(id) = self.types.kind(canonical) else {
206 self.unsupported("a call through something that is not a function", span);
207 return None;
208 };
209 let signature = self.types.signature(id);
210 let ret = signature.ret;
211 let variadic = signature.variadic || !signature.prototyped;
215 let params = signature.params.clone();
216
217 match abi::plan(self.types, self.target, ret, ¶ms, actual, variadic) {
218 Ok(plan) => Some(plan),
219 Err(what) => {
220 self.unsupported(what, span);
221 None
222 }
223 }
224 }
225
226 pub(crate) fn image(&mut self, init: Option<InitList>, size: u64, span: Span) -> DataList {
228 let Some(init) = init else { return self.zeros(size) };
229 let entries: Vec<InitEntry> = self.tast[init].to_vec();
230 let mut packed = self.packed(&entries, size);
231 let mut data: Vec<Datum> = Vec::with_capacity(entries.len());
232 let mut at = 0;
233 for entry in entries {
234 let Some(datum) = self.entry(entry, &mut packed, size) else { continue };
235 match entry.offset.cmp(&at) {
236 Ordering::Greater => data.push(Datum::Zero(entry.offset - at)),
237 Ordering::Less => {
241 self.unsupported("an initializer that writes over an earlier one", span);
242 continue;
243 }
244 Ordering::Equal => {}
245 }
246 at = entry.offset + datum.size(&self.module);
247 data.push(datum);
248 }
249 if at < size {
250 data.push(Datum::Zero(size - at));
253 }
254 self.module.push_data(&data)
255 }
256
257 fn entry(
264 &mut self,
265 entry: InitEntry,
266 packed: &mut BTreeMap<u64, u8>,
267 size: u64,
268 ) -> Option<Datum> {
269 if entry.is_bit_field() {
270 let bytes = take_run(packed, entry.offset)?;
271 return Some(Datum::Bytes(self.module.push_bytes(&bytes)));
272 }
273 let room = size.saturating_sub(entry.offset);
274 self.datum(entry.value, room)
275 }
276
277 fn packed(&mut self, entries: &[InitEntry], size: u64) -> BTreeMap<u64, u8> {
282 let mut bytes = BTreeMap::new();
283 for entry in entries.iter().filter(|entry| entry.is_bit_field()) {
284 let Some(folded) = self.fold(entry.value) else { continue };
285 let Const::Int(number) = folded else {
286 let span = self.tast.expr_span(entry.value);
287 let what = "a bit-field initialized by something that is not an integer";
288 self.unsupported(what, span);
289 continue;
290 };
291 let width = entry.bit_width;
292 let ones = if width >= 128 { u128::MAX } else { (1u128 << width) - 1 };
293 let mut placed = ((number as u128) & ones) << entry.bit_offset;
294 let mut at = entry.offset;
295 while placed != 0 && at < size {
296 *bytes.entry(at).or_insert(0) |= (placed & 0xff) as u8;
297 placed >>= 8;
298 at += 1;
299 }
300 }
301 bytes
302 }
303
304 fn datum(&mut self, value: ExprId, room: u64) -> Option<Datum> {
306 let tast = self.tast;
307 let ty = tast[value].ty;
308 let span = tast.expr_span(value);
309 if let TypeKind::Array { .. } = self.types.kind(self.types.canonical(ty)) {
310 let ExprKind::Str(id) = tast[value].kind else {
314 self.unsupported("this initializer", span);
315 return None;
316 };
317 let bytes = tast[id].bytes(self.target);
318 let take = bytes.len().min(usize::try_from(room).unwrap_or(usize::MAX));
319 return Some(Datum::Bytes(self.module.push_bytes(&bytes[..take])));
320 }
321
322 let size = repr::size_of(self.types, self.target, ty);
323 match self.fold(value)? {
324 Const::Int(number) => {
325 let ty = repr::value_type(self.types, self.target, ty)?;
326 let ty = if ty.is_ptr() { Type::int(self.target.pointer_width) } else { ty };
332 let imm = self.module.add_imm(Imm::int(number, ty));
333 Some(Datum::Scalar { ty, value: imm })
334 }
335 Const::Float(number) => {
336 let ty = repr::value_type(self.types, self.target, ty)?;
337 let imm = self.module.add_imm(Imm::from_bits(number.to_bits()));
338 Some(Datum::Scalar { ty, value: imm })
339 }
340 Const::Address(address) => {
341 let symbol = match address.base {
342 Base::Decl(decl) => self.symbol_of(decl),
343 Base::Str(id) => self.string(id),
344 };
345 let addend = i64::try_from(address.offset).unwrap_or(0);
346 let size = u32::try_from(size).unwrap_or(0);
347 Some(Datum::Addr(self.module.add_reloc(Reloc { symbol, addend, size })))
348 }
349 }
350 }
351
352 fn zeros(&mut self, size: u64) -> DataList {
354 if size == 0 {
355 return DataList::EMPTY;
356 }
357 self.module.push_data(&[Datum::Zero(size)])
358 }
359
360 pub(crate) fn string(&mut self, id: StrId) -> Symbol {
362 if let Some(&symbol) = self.strings.get(&id) {
363 return symbol;
364 }
365 let literal = &self.tast[id];
366 let bytes = literal.bytes(self.target);
367 let align = literal.encoding.element_width(self.target) / 8;
368 let symbol = self.names.intern(&format!(".Lstr.{}", self.strings.len()));
369
370 let mut global = Global::new(symbol, bytes.len() as u64, align.max(1));
371 global.linkage = IrLinkage::Internal;
372 global.constant = true;
376 let range = self.module.push_bytes(&bytes);
377 global.init = Some(self.module.push_data(&[Datum::Bytes(range)]));
378 self.module.add_global(global);
379 self.strings.insert(id, symbol);
380 symbol
381 }
382
383 pub(crate) fn symbol_of(&mut self, decl: DeclId) -> Symbol {
385 let tast = self.tast;
386 let node = &tast[decl];
387 if node.linkage != Linkage::None {
388 return node.name.unwrap_or_else(|| self.names.intern(".Lanon"));
389 }
390 if let Some(&symbol) = self.statics.get(&decl) {
391 return symbol;
392 }
393 let base = match node.name {
396 Some(name) => self.names.resolve(name).to_string(),
397 None => ".Lanon".to_string(),
398 };
399 let symbol = self.names.intern(&format!("{base}.{}", self.statics.len()));
400 self.statics.insert(decl, symbol);
401 symbol
402 }
403
404 pub(crate) fn local_static(&mut self, decl: DeclId) {
406 if !self.done.insert(decl) {
407 return;
408 }
409 match self.tast[decl].kind {
410 DeclKind::Function => self.function(decl),
413 DeclKind::Object => self.object(decl),
414 }
415 }
416
417 fn fold(&mut self, expr: ExprId) -> Option<Const> {
419 let mut eval = Eval::new(self.tast, self.types, self.target, self.names);
420 let folded = eval.constant(expr);
421 let reported = eval.finish();
422 self.diagnostics.extend(reported);
423 match folded {
424 Ok(value) => Some(value),
425 Err(stop) => {
426 if !stop.poisoned {
427 let span = self.tast.expr_span(stop.at);
428 self.unsupported("an initializer this compiler cannot fold", span);
429 }
430 None
431 }
432 }
433 }
434
435 pub(crate) fn unsupported(&mut self, what: &str, span: Span) {
437 self.diagnostics.push(
438 Diagnostic::error(format!("{what} is not supported yet"), span).with_code("E0519"),
439 );
440 }
441}
442
443fn take_run(bytes: &mut BTreeMap<u64, u8>, start: u64) -> Option<Vec<u8>> {
448 let mut run = vec![bytes.remove(&start)?];
449 let mut at = start + 1;
450 while let Some(byte) = bytes.remove(&at) {
451 run.push(byte);
452 at += 1;
453 }
454 Some(run)
455}