diff --git a/src/ast.rs b/src/ast.rs index 44e5989..91021b8 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -370,30 +370,30 @@ impl<'a> AstBuilderImpl<'a> { error_logger, tokens, cur: 0, - offset_size: 0, - loop_stack: vec![], - labels: HashMap::new(), - defined_labels: HashSet::new(), - switch_stack: vec![], - switch_types: vec![], - return_type: None, + scope_stack: vec![reserved_functions()], + struct_definitions: vec![], + next_enum_id: 0, + declaration_attributes_state: None, + parameter_depth: 0, current_function: None, - mutable_static: None, + return_type: None, return_pointer: None, + mutable_static: None, variadic_parameters: None, variadic_last: None, + offset_size: 0, + next_deferred_local: usize::MAX / 2, pending_bounds: vec![], - parameter_depth: 0, - declaration_attributes_state: None, dynamic_objects: vec![], vla_active: vec![], - jump_depths: vec![], + loop_stack: vec![], continue_depths: HashMap::new(), + switch_stack: vec![], + switch_types: vec![], switch_vla_depths: vec![], - next_enum_id: 0, - next_deferred_local: usize::MAX / 2, - struct_definitions: vec![], - scope_stack: vec![reserved_functions()], + labels: HashMap::new(), + defined_labels: HashSet::new(), + jump_depths: vec![], functions: HashMap::new(), global_variables: HashMap::new(), string_literals: Vec::new(), @@ -456,7 +456,7 @@ impl<'a> AstBuilderImpl<'a> { self.declaration_attributes(false, None, true); if matches!( self.tokens[self.cur].tt, - TokenType::Ident | TokenType::Reserved + TokenType::Reserved | TokenType::Ident ) && matches!( self.tokens[self.cur].s_value.as_str(), "asm" | "__asm" | "__asm__" @@ -483,12 +483,14 @@ impl<'a> AstBuilderImpl<'a> { .labels .entry(token.s_value.to_string()) .or_insert(token.pos); + let vla_scope = self.vla_active.clone(); + let lhs = Some(Box::new(self.stmt())); return Node { nt: NodeType::Label, - vla_scope: self.vla_active.clone(), - value: Some(id), token: Some(token), - lhs: Some(Box::new(self.stmt())), + lhs, + value: Some(id), + vla_scope, ..Node::default() }; } @@ -505,10 +507,10 @@ impl<'a> AstBuilderImpl<'a> { self.expect_reserved(";"); return Node { nt: NodeType::Goto, + token: Some(token.clone()), + value: Some(id), args: self.dynamic_objects.clone(), vla_scope: self.vla_active.clone(), - value: Some(id), - token: Some(token.clone()), ..Node::default() }; } @@ -551,8 +553,8 @@ impl<'a> AstBuilderImpl<'a> { self.switch_vla_depths.pop(); self.jump_depths.pop(); return Node { - token: Some(t.clone()), nt: NodeType::Switch, + token: Some(t.clone()), cond: Some(Box::new(cond)), then: Some(Box::new(body)), children: cases, @@ -789,15 +791,15 @@ impl<'a> AstBuilderImpl<'a> { unreachable!() }); Node { - token: Some(t.clone()), nt: NodeType::Continue, + token: Some(t.clone()), value: Some(target), ..Node::default() } } else if let Some(t) = self.attempt_reserved("break") { Node { - token: Some(t.clone()), nt: NodeType::Break, + token: Some(t.clone()), ..Node::default() } } else if let Some(t) = self.attempt_reserved("return") { @@ -827,10 +829,10 @@ impl<'a> AstBuilderImpl<'a> { } } Node { - token: Some(t.clone()), nt: NodeType::Return, - offset: self.return_pointer, + token: Some(t.clone()), lhs: operand.map(Box::new), + offset: self.return_pointer, ..Node::default() } } else { @@ -844,8 +846,8 @@ impl<'a> AstBuilderImpl<'a> { let (token, mut children, cleanup) = self.consume_block_parts()?; children.extend(cleanup); Some(Node { - token: Some(token), nt: NodeType::Block, + token: Some(token), children, ..Node::default() }) @@ -1117,15 +1119,15 @@ impl<'a> AstBuilderImpl<'a> { children.push(Self::runtime_size(&ty)); Node { nt: NodeType::Sequence, - children, token: Some(token.clone()), + children, ..Node::default() } } else { Node { token: Some(token.clone()), - value: Some(ty.size_of()), cty: Some(Type::integer(64, false)), + value: Some(ty.size_of()), ..Node::default() } } @@ -1164,8 +1166,8 @@ impl<'a> AstBuilderImpl<'a> { Node { nt: NodeType::Sequence, token: Some(token.clone()), - compound_literal: true, children, + compound_literal: true, ..Node::default() } } else { @@ -1183,19 +1185,19 @@ impl<'a> AstBuilderImpl<'a> { GlobalVariable { ty: ty.clone(), alignment: 0, - thread_local: false, data: Some(data), internal: true, + declaration_only: false, + common: false, weak_import: false, hidden: false, - common: false, - declaration_only: false, + thread_local: false, }, ); Node { nt: NodeType::GlobalVar, - cty: Some(ty), token: Some(token.clone()), + cty: Some(ty), global_name: name.s_value.to_string(), ..Node::default() } @@ -1214,13 +1216,13 @@ impl<'a> AstBuilderImpl<'a> { if !(ty.is_arithmetic() || matches!(ty, Type::Void | Type::Ptr(_))) || (!matches!( source.unqualified(), - Type::I8 - | Type::Enum(_) + Type::Bool + | Type::I8 | Type::SignedChar - | Type::LongLong { .. } | Type::I32 | Type::Integer { .. } - | Type::Bool + | Type::LongLong { .. } + | Type::Enum(_) | Type::Float(_) | Type::LongDouble(_) | Type::Ptr(_) @@ -1310,8 +1312,8 @@ impl<'a> AstBuilderImpl<'a> { self.expect_reserved(")"); return Node { token: Some(token.clone()), - value: Some(ty.align_of()), cty: Some(Type::integer(64, false)), + value: Some(ty.align_of()), ..Node::default() }; } @@ -1324,9 +1326,9 @@ impl<'a> AstBuilderImpl<'a> { .filter(|ty| ty.float_bits().is_some()) { let minus_one = Node { + token: Some(t.clone()), cty: Some(ty.clone()), value: Some(ty.float_constant(-1.0)), - token: Some(t.clone()), ..Node::default() }; return Node::new_with_op(Some(t.clone()), NodeType::Mul, operand, minus_one); @@ -1373,10 +1375,11 @@ impl<'a> AstBuilderImpl<'a> { return Node::new_with_op_and_lhs(Some(t.clone()), NodeType::BitNot, self.unary()); } if let Some(t) = self.attempt_reserved("!") { + let lhs = Some(Box::new(self.unary())); return Node { - token: Some(t.clone()), nt: NodeType::Eq, - lhs: Some(Box::new(self.unary())), + token: Some(t.clone()), + lhs, rhs: Some(Box::new(Node::new_with_num(None, 0))), ..Node::default() }; @@ -1513,24 +1516,28 @@ impl<'a> AstBuilderImpl<'a> { crate::token::StringEncoding::Wide => Type::wide_character(), }; let count = t.encoded_string().len() / t.string_encoding.unit_size(); + let dest = self.new_string_literal(&t); Node { - token: Some(t.clone()), nt: NodeType::GlobalVar, + token: Some(t.clone()), cty: Some(Type::Arr(Rc::new(element_type), Some(count))), - dest: self.new_string_literal(&t), + dest, ..Node::default() } } else if let Some(t) = self.attempt(TokenType::Float) { + let value = Some(t.i_value); + let extended_float = t.extended_float.clone(); + let cty = Some(if t.s_value == "l" { + self.long_double_type(&t) + } else { + Type::Float(if t.s_value == "f" { 32 } else { 64 }) + }); Node { nt: NodeType::Num, - value: Some(t.i_value), - extended_float: t.extended_float.clone(), - cty: Some(if t.s_value == "l" { - self.long_double_type(&t) - } else { - Type::Float(if t.s_value == "f" { 32 } else { 64 }) - }), token: Some(t), + cty, + value, + extended_float, ..Node::default() } } else if let Some(t) = self.attempt(TokenType::Num) { @@ -1552,10 +1559,10 @@ impl<'a> AstBuilderImpl<'a> { t.i_value }; Node { - token: Some(t), nt: NodeType::Num, - value: Some(value), + token: Some(t), cty: Some(ty), + value: Some(value), ..Node::default() } } else if let Some(t) = self.attempt_ident() { @@ -1683,8 +1690,8 @@ impl<'a> AstBuilderImpl<'a> { return Node { nt: NodeType::StackAlloc, token: Some(t), - lhs: Some(Box::new(size)), cty: Some(Type::Ptr(Rc::new(Type::Void))), + lhs: Some(Box::new(size)), ..Node::default() }; } @@ -1714,23 +1721,40 @@ impl<'a> AstBuilderImpl<'a> { .is_some_and(|object| object.thread_local); if let Some(ident) = self.resolve_name(&t.s_value) { match ident { + Identifier::TypeDef(..) | Identifier::EnumTag { .. } => { + self.error_logger + .print_error_position(t.pos, "type name is not an expression"); + unreachable!() + } + Identifier::Constant(value, ty) => Node { + cty: Some(ty.clone()), + ..Node::new_with_num(Some(t.clone()), *value as usize) + }, + Identifier::Local(ty, offset, registered, alignment) => Node { + nt: NodeType::LocalVar, + token: Some(t.clone()), + cty: Some(ty.clone()), + offset: Some(*offset), + alignment: *alignment, + registered: *registered, + ..Node::default() + }, Identifier::Dynamic(ty, offset, registered) => { let mut node = Self::dynamic_node(ty.clone(), *offset, t.clone()); node.registered = *registered; node } - Identifier::Local(ty, offset, registered, alignment) => Node { - registered: *registered, - alignment: *alignment, + Identifier::Static(ty) => Node { + nt: NodeType::GlobalVar, token: Some(t.clone()), - nt: NodeType::LocalVar, cty: Some(ty.clone()), - offset: Some(*offset), + global_name: t.s_value.to_string(), + thread_local, ..Node::default() }, Identifier::StaticObject(ty, name) => Node { - token: Some(t.clone()), nt: NodeType::GlobalVar, + token: Some(t.clone()), cty: Some(ty.clone()), global_name: name.clone(), thread_local: self @@ -1739,23 +1763,6 @@ impl<'a> AstBuilderImpl<'a> { .is_some_and(|object| object.thread_local), ..Node::default() }, - Identifier::Static(ty) => Node { - token: Some(t.clone()), - nt: NodeType::GlobalVar, - cty: Some(ty.clone()), - global_name: t.s_value.to_string(), - thread_local, - ..Node::default() - }, - Identifier::Constant(value, ty) => Node { - cty: Some(ty.clone()), - ..Node::new_with_num(Some(t.clone()), *value as usize) - }, - Identifier::TypeDef(..) | Identifier::EnumTag { .. } => { - self.error_logger - .print_error_position(t.pos, "type name is not an expression"); - unreachable!() - } } } else { self.error_logger @@ -1816,6 +1823,7 @@ impl<'a> AstBuilderImpl<'a> { target.clone(), ); let one = Node { + token: Some(token.clone()), cty: Some(if ty.float_bits().is_some() { ty.clone() } else { @@ -1826,7 +1834,6 @@ impl<'a> AstBuilderImpl<'a> { } else { 1 }), - token: Some(token.clone()), ..Node::default() }; let value = Node::new_with_op( @@ -1920,15 +1927,15 @@ impl<'a> AstBuilderImpl<'a> { }; let callee = if direct { None } else { Some(Box::new(node)) }; node = Node { - token: Some(t), nt: NodeType::CallFunc, - offset: result_offset, - global_name: s_value, + token: Some(t), cty: Some(return_type), + lhs: callee, + global_name: s_value, args, parameter_types, variadic, - lhs: callee, + offset: result_offset, ..Node::default() } } else if let Some(token) = self @@ -1973,8 +1980,8 @@ impl<'a> AstBuilderImpl<'a> { node = Node::new_with_op(Some(b_token.clone()), NodeType::Add, node, self.expr()); self.expect_reserved("]"); node = Node { - token: Some(b_token.clone()), nt: NodeType::Deref, + token: Some(b_token.clone()), lhs: Some(Box::new(node)), ..Node::default() } @@ -2147,29 +2154,29 @@ mod tests { Token { tt: TokenType::Ident, pos: 0, - i_value: 0, s_value: "x".to_string().into(), + i_value: 0, ..Token::default() }, Token { tt: TokenType::Reserved, pos: 0, - i_value: 0, s_value: "=".to_string().into(), + i_value: 0, ..Token::default() }, Token { tt: TokenType::Num, pos: 0, - i_value: 42, s_value: String::new().into(), + i_value: 42, ..Token::default() }, Token { tt: TokenType::Reserved, pos: 0, - i_value: 0, s_value: ";".to_string().into(), + i_value: 0, ..Token::default() }, ]; diff --git a/src/ast/atomic.rs b/src/ast/atomic.rs index 8f21978..88c4efa 100644 --- a/src/ast/atomic.rs +++ b/src/ast/atomic.rs @@ -82,14 +82,14 @@ impl AstBuilderImpl<'_> { args.push(order); self.postfix(Node { nt: NodeType::Atomic(operation), + token: Some(token), cty: Some(if operation == AtomicOperation::Store { Type::Void } else { ty }), - volatile_access: object.qualifiers().volatile, - token: Some(token), args, + volatile_access: object.qualifiers().volatile, ..Node::default() }) } @@ -116,8 +116,8 @@ impl AstBuilderImpl<'_> { if matches!(ty, Type::Ptr(_)) { Node { nt: NodeType::Cast, - cty: Some(ty.clone()), token: value.token.clone(), + cty: Some(ty.clone()), lhs: Some(Box::new(value)), ..Node::default() } @@ -190,10 +190,10 @@ impl AstBuilderImpl<'_> { nt: NodeType::Atomic(AtomicOperation::CompareExchange( expected_object.qualifiers().volatile, )), - cty: Some(Type::Bool), token: Some(token), - volatile_access: object.qualifiers().volatile, + cty: Some(Type::Bool), args: vec![pointer, expected, desired, weak, success, failure], + volatile_access: object.qualifiers().volatile, ..Node::default() }) } diff --git a/src/ast/attributes.rs b/src/ast/attributes.rs index 493706f..9864eb4 100644 --- a/src/ast/attributes.rs +++ b/src/ast/attributes.rs @@ -95,7 +95,7 @@ impl AstBuilderImpl<'_> { continue; } let name = self.tokens[self.cur].clone(); - if !matches!(name.tt, TokenType::Ident | TokenType::Reserved) { + if !matches!(name.tt, TokenType::Reserved | TokenType::Ident) { self.error_logger .print_error_position(name.pos, "attribute name expected"); } diff --git a/src/ast/builtins.rs b/src/ast/builtins.rs index cbf8f36..d072f58 100644 --- a/src/ast/builtins.rs +++ b/src/ast/builtins.rs @@ -85,8 +85,8 @@ impl AstBuilderImpl<'_> { self.expect_reserved(")"); Node { token: Some(token), - value: Some(offset), cty: Some(Type::integer(64, false)), + value: Some(offset), ..Node::default() } } @@ -111,12 +111,6 @@ impl AstBuilderImpl<'_> { .or_insert_with(|| GlobalVariable { ty: ty.clone(), alignment: 1, - thread_local: false, - internal: true, - weak_import: false, - hidden: false, - common: false, - declaration_only: false, data: Some(GlobalVariableData::Arr( function .s_value @@ -125,12 +119,18 @@ impl AstBuilderImpl<'_> { .map(|byte| GlobalVariableData::Elem(byte.to_string())) .collect(), )), + internal: true, + declaration_only: false, + common: false, + weak_import: false, + hidden: false, + thread_local: false, }); Node { nt: NodeType::GlobalVar, - global_name: name, token: Some(token), cty: Some(ty), + global_name: name, ..Node::default() } } @@ -146,9 +146,9 @@ impl AstBuilderImpl<'_> { self.expect_conversion(&ty, &expected); let cast = |operand| Node { nt: NodeType::Cast, + token: Some(token.clone()), cty: Some(ty.clone()), lhs: Some(Box::new(operand)), - token: Some(token.clone()), ..Node::default() }; let value = cast(value); @@ -190,10 +190,11 @@ impl AstBuilderImpl<'_> { }; self.expect_reserved("("); self.expect_reserved(")"); + let value = ty.float_constant(f64::INFINITY); Node { token: Some(token), - value: Some(ty.float_constant(f64::INFINITY)), cty: Some(ty), + value: Some(value), ..Node::default() } } @@ -232,13 +233,13 @@ impl AstBuilderImpl<'_> { } else { 0x7ff8_0000_0000_0000 | (payload & 0x0007_ffff_ffff_ffff) }; + let extended_float = (ty.float_bits() == Some(80)) + .then(|| Box::new(crate::floating::Extended::qnan(Some(payload.into())).to_bits())); return Node { token: Some(token), - value: Some(bits as usize), - extended_float: (ty.float_bits() == Some(80)).then(|| { - Box::new(crate::floating::Extended::qnan(Some(payload.into())).to_bits()) - }), cty: Some(ty), + value: Some(bits as usize), + extended_float, ..Node::default() }; } @@ -389,8 +390,8 @@ impl AstBuilderImpl<'_> { } } self.functions.entry(name.into()).or_insert_with(|| Func { - cty: ty.clone(), token: Some(token.clone()), + cty: ty.clone(), ..Func::default() }); self.scope_stack[0] @@ -398,8 +399,8 @@ impl AstBuilderImpl<'_> { .or_insert_with(|| Identifier::Static(ty.clone())); self.postfix(Node { nt: NodeType::GlobalVar, - cty: Some(ty), token: Some(token), + cty: Some(ty), global_name: name.into(), ..Node::default() }) diff --git a/src/ast/const_eval.rs b/src/ast/const_eval.rs index 1c2e7fd..189374c 100644 --- a/src/ast/const_eval.rs +++ b/src/ast/const_eval.rs @@ -29,21 +29,20 @@ impl AstBuilderImpl<'_> { .is_some_and(|ty| ty.integer_info().is_some()) && node.lhs.as_deref().is_some_and(Self::integer_constant) } - NodeType::BitNot => node.lhs.as_deref().is_some_and(Self::integer_constant), NodeType::Add | NodeType::Sub | NodeType::Mul | NodeType::Div | NodeType::Mod + | NodeType::BitLeft + | NodeType::BitRight + | NodeType::BitAnd + | NodeType::BitXor + | NodeType::BitOr | NodeType::Eq | NodeType::Ne | NodeType::Lt | NodeType::Le - | NodeType::BitAnd - | NodeType::BitOr - | NodeType::BitXor - | NodeType::BitLeft - | NodeType::BitRight | NodeType::LogicalAnd | NodeType::LogicalOr => { node.lhs @@ -54,6 +53,7 @@ impl AstBuilderImpl<'_> { .as_ref() .is_some_and(|child| Self::integer_constant(child)) } + NodeType::BitNot => node.lhs.as_deref().is_some_and(Self::integer_constant), NodeType::Conditional => [&node.cond, &node.then, &node.els] .into_iter() .flatten() @@ -241,7 +241,24 @@ impl AstBuilderImpl<'_> { } }; } - + NodeType::Add + | NodeType::Sub + | NodeType::Mul + | NodeType::Div + | NodeType::Mod + | NodeType::BitLeft + | NodeType::BitRight + | NodeType::BitAnd + | NodeType::BitXor + | NodeType::BitOr + | NodeType::Eq + | NodeType::Ne + | NodeType::Lt + | NodeType::Le => {} + NodeType::BitNot => { + let value = !self.eval(node.lhs.as_ref().unwrap()); + return node.resolve_type().unwrap().convert_constant(value); + } NodeType::LogicalAnd => { return (self.eval_truth(node.lhs.as_ref().unwrap()) && self.eval_truth(node.rhs.as_ref().unwrap())) as i64; @@ -257,24 +274,6 @@ impl AstBuilderImpl<'_> { self.eval(node.els.as_ref().unwrap()) }; } - NodeType::BitNot => { - let value = !self.eval(node.lhs.as_ref().unwrap()); - return node.resolve_type().unwrap().convert_constant(value); - } - NodeType::Eq - | NodeType::Ne - | NodeType::Le - | NodeType::Lt - | NodeType::Add - | NodeType::Sub - | NodeType::Mul - | NodeType::Div - | NodeType::Mod - | NodeType::BitAnd - | NodeType::BitOr - | NodeType::BitXor - | NodeType::BitLeft - | NodeType::BitRight => {} _ => { self.error_logger.print_error_position( node.token.as_ref().unwrap().pos, @@ -294,14 +293,14 @@ impl AstBuilderImpl<'_> { if unsigned { let (lhs, rhs) = (lhs as u64, rhs as u64); let value = match node.nt { - NodeType::Div if rhs != 0 => Some(lhs / rhs), - NodeType::Mod if rhs != 0 => Some(lhs % rhs), - NodeType::Lt => Some(u64::from(lhs < rhs)), - NodeType::Le => Some(u64::from(lhs <= rhs)), NodeType::Add => Some(lhs.wrapping_add(rhs)), NodeType::Sub => Some(lhs.wrapping_sub(rhs)), NodeType::Mul => Some(lhs.wrapping_mul(rhs)), + NodeType::Div if rhs != 0 => Some(lhs / rhs), + NodeType::Mod if rhs != 0 => Some(lhs % rhs), NodeType::BitRight if rhs < ty.integer_info().unwrap().0 as u64 => Some(lhs >> rhs), + NodeType::Lt => Some(u64::from(lhs < rhs)), + NodeType::Le => Some(u64::from(lhs <= rhs)), _ => None, }; if let Some(value) = value { @@ -309,18 +308,11 @@ impl AstBuilderImpl<'_> { } } let result = match node.nt { - NodeType::Eq => Some((lhs == rhs) as i64), - NodeType::Ne => Some((lhs != rhs) as i64), - NodeType::Le => Some((lhs <= rhs) as i64), - NodeType::Lt => Some((lhs < rhs) as i64), NodeType::Add => lhs.checked_add(rhs), NodeType::Sub => lhs.checked_sub(rhs), NodeType::Mul => lhs.checked_mul(rhs), NodeType::Div => lhs.checked_div(rhs), NodeType::Mod => lhs.checked_rem(rhs), - NodeType::BitAnd => Some(lhs & rhs), - NodeType::BitOr => Some(lhs | rhs), - NodeType::BitXor => Some(lhs ^ rhs), NodeType::BitLeft if (0..lhs_ty.promoted().integer_info().unwrap().0 as i64).contains(&rhs) => { @@ -331,6 +323,13 @@ impl AstBuilderImpl<'_> { { lhs.checked_shr(rhs as u32) } + NodeType::BitAnd => Some(lhs & rhs), + NodeType::BitXor => Some(lhs ^ rhs), + NodeType::BitOr => Some(lhs | rhs), + NodeType::Eq => Some((lhs == rhs) as i64), + NodeType::Ne => Some((lhs != rhs) as i64), + NodeType::Lt => Some((lhs < rhs) as i64), + NodeType::Le => Some((lhs <= rhs) as i64), _ => None, }; let value = result.unwrap_or_else(|| { @@ -388,7 +387,6 @@ impl AstBuilderImpl<'_> { } let value = match node.nt { NodeType::Num => ty.float_value(node.value.unwrap()), - NodeType::FloatAbs => self.eval_float_as(node.lhs.as_ref().unwrap(), &ty).abs(), NodeType::Cast => self.eval_float(node.lhs.as_ref().unwrap()), NodeType::Add => { self.eval_float_as(node.lhs.as_ref().unwrap(), &ty) @@ -413,6 +411,7 @@ impl AstBuilderImpl<'_> { self.eval_float(node.els.as_ref().unwrap()) } } + NodeType::FloatAbs => self.eval_float_as(node.lhs.as_ref().unwrap(), &ty).abs(), _ => { self.error_logger.print_error_position( node.token.as_ref().unwrap().pos, @@ -449,7 +448,6 @@ impl AstBuilderImpl<'_> { |bits| Extended::from_bits(**bits), ), NodeType::Cast => self.eval_extended(node.lhs.as_ref().unwrap()), - NodeType::FloatAbs => self.eval_extended(node.lhs.as_ref().unwrap()).abs(), NodeType::Add | NodeType::Sub | NodeType::Mul | NodeType::Div => { let lhs = self.eval_extended(node.lhs.as_ref().unwrap()); let rhs = self.eval_extended(node.rhs.as_ref().unwrap()); @@ -474,6 +472,7 @@ impl AstBuilderImpl<'_> { node.els.as_ref().unwrap() }) } + NodeType::FloatAbs => self.eval_extended(node.lhs.as_ref().unwrap()).abs(), _ => { self.error_logger.print_error_position( node.token.as_ref().unwrap().pos, diff --git a/src/ast/declarations.rs b/src/ast/declarations.rs index ca3f555..e23739a 100644 --- a/src/ast/declarations.rs +++ b/src/ast/declarations.rs @@ -84,11 +84,11 @@ impl AstBuilderImpl<'_> { Identifier::Local(ty.clone(), self.offset_size, false, alignment), ); Node { - token: Some(t), nt: NodeType::LocalVar, - alignment, + token: Some(t), cty: Some(ty), offset: Some(self.offset_size), + alignment, ..Node::default() } } @@ -483,8 +483,8 @@ impl AstBuilderImpl<'_> { self.functions .entry(token.s_value.to_string()) .or_insert_with(|| Func { - cty: ty.clone(), token: Some(token.clone()), + cty: ty.clone(), ..Func::default() }); let internal = internal @@ -523,15 +523,15 @@ impl AstBuilderImpl<'_> { if !definition { if function.body.is_none() { *function = Func { - gnu_inline, - noinline, - weak_import, - hidden, - external_definition, - cty: ty, token: Some(token), + cty: ty, offset_size: self.offset_size, internal, + external_definition, + weak_import, + hidden, + gnu_inline, + noinline, ..Func::default() }; } @@ -610,25 +610,26 @@ impl AstBuilderImpl<'_> { return; } } - self.functions.insert( - token.s_value.to_string(), + self.functions.insert(token.s_value.to_string(), { + let inline_definition = inline && body.is_some(); + let mutable_static = self.mutable_static.take(); Func { - inline_definition: inline && body.is_some(), - gnu_inline, - noinline, - weak_import, - hidden, - external_definition, - mutable_static: self.mutable_static.take(), - body, - cty: ty, token: Some(token), + cty: ty, args, + body, + mutable_static, offset_size: self.offset_size, - internal, return_pointer: self.return_pointer, - }, - ); + internal, + external_definition, + weak_import, + hidden, + inline_definition, + gnu_inline, + noinline, + } + }); return; } if let Some(previous) = self.global_variables.get(token.s_value.as_str()) { @@ -648,13 +649,13 @@ impl AstBuilderImpl<'_> { .or_insert_with(|| GlobalVariable { ty: ty.clone(), alignment, - thread_local, + data: None, + internal, + declaration_only: external, + common: attributes.common, weak_import: attributes.weak_import, hidden: attributes.hidden, - common: attributes.common, - declaration_only: external, - internal, - data: None, + thread_local, }); let data = if self.attempt_reserved("=").is_some() { Some(self.global_initializer(&ty)) @@ -841,8 +842,8 @@ impl AstBuilderImpl<'_> { self.functions.insert( name.clone(), Func { - cty: ty.clone(), token: Some(token.clone()), + cty: ty.clone(), ..Func::default() }, ); @@ -887,13 +888,13 @@ impl AstBuilderImpl<'_> { .or_insert_with(|| GlobalVariable { ty: ty.clone(), alignment: requested_alignment, - thread_local, + data: None, internal: !external, + declaration_only: external, + common: false, weak_import: false, hidden: false, - common: false, - declaration_only: external, - data: None, + thread_local, }); let data = if self.attempt_reserved("=").is_some() { if external { diff --git a/src/ast/declarators.rs b/src/ast/declarators.rs index eb5c519..4e5161d 100644 --- a/src/ast/declarators.rs +++ b/src/ast/declarators.rs @@ -198,8 +198,8 @@ impl AstBuilderImpl<'_> { matches!( identifier, Identifier::TypeDef(_) - | Identifier::Constant(..) | Identifier::EnumTag { .. } + | Identifier::Constant(..) ) }) .collect(); diff --git a/src/ast/initializers.rs b/src/ast/initializers.rs index 45b20f5..279797d 100644 --- a/src/ast/initializers.rs +++ b/src/ast/initializers.rs @@ -637,8 +637,8 @@ impl AstBuilderImpl<'_> { nt: NodeType::ZeroFill { bytes: size * unit_size, }, - cty: Some(Type::Void), token: Some(token.clone()), + cty: Some(Type::Void), lhs: Some(Box::new(node.clone())), ..Node::default() }); @@ -647,8 +647,8 @@ impl AstBuilderImpl<'_> { nt: NodeType::CopyBytes { bytes: size.min(literal_size) * unit_size, }, - cty: Some(Type::Void), token: Some(token.clone()), + cty: Some(Type::Void), lhs: Some(Box::new(node.clone())), rhs: Some(Box::new(literal)), ..Node::default() @@ -779,8 +779,8 @@ impl AstBuilderImpl<'_> { } } Node { - token: Some(assign_token.clone()), nt: NodeType::Block, + token: Some(assign_token.clone()), children, ..Node::default() } @@ -805,8 +805,8 @@ impl AstBuilderImpl<'_> { nt: NodeType::CopyBytes { bytes: ty.size_of(), }, - cty: Some(ty), token: Some(token), + cty: Some(ty), lhs: Some(Box::new(lhs)), rhs: Some(Box::new(rhs)), ..Node::default() @@ -872,7 +872,7 @@ impl AstBuilderImpl<'_> { } pub(super) fn initializer_expressions(node: Node, expressions: &mut Vec) { - if matches!(node.nt, NodeType::Block | NodeType::DefVar) { + if matches!(node.nt, NodeType::DefVar | NodeType::Block) { for child in node.children { Self::initializer_expressions(child, expressions); } diff --git a/src/ast/inline_asm.rs b/src/ast/inline_asm.rs index aea074e..b2f08a9 100644 --- a/src/ast/inline_asm.rs +++ b/src/ast/inline_asm.rs @@ -86,8 +86,8 @@ impl AstBuilderImpl<'_> { if matches!(input_type, Type::Ptr(_)) { input = Node { nt: NodeType::Cast, - cty: Some(input_type), token: input.token.clone(), + cty: Some(input_type), lhs: Some(Box::new(input)), ..Node::default() }; @@ -151,12 +151,12 @@ impl AstBuilderImpl<'_> { let volatile = volatile || destinations.is_empty(); children.push(Node { nt: NodeType::InlineAsm(volatile, memory), - dest: template, - children: destinations, - args: inputs, - value: Some(read_write), token: Some(token.clone()), cty: Some(Type::Void), + children: destinations, + value: Some(read_write), + dest: template, + args: inputs, ..Node::default() }); Node { diff --git a/src/ast/inline_calls.rs b/src/ast/inline_calls.rs index f6b7407..98e9764 100644 --- a/src/ast/inline_calls.rs +++ b/src/ast/inline_calls.rs @@ -72,17 +72,21 @@ impl AstBuilderImpl<'_> { for name in referenced { let local = format!(".inline.{name}"); let function = self.functions.get_mut(&name).unwrap(); - let local_function = Func { - body: function.body.take(), - internal: true, - noinline: function.noinline, - external_definition: true, - cty: function.cty.clone(), - args: std::mem::take(&mut function.args), - offset_size: function.offset_size, - return_pointer: function.return_pointer, - token: function.token.clone(), - ..Func::default() + let local_function = { + let body = function.body.take(); + let args = std::mem::take(&mut function.args); + Func { + token: function.token.clone(), + cty: function.cty.clone(), + args, + body, + offset_size: function.offset_size, + return_pointer: function.return_pointer, + internal: true, + external_definition: true, + noinline: function.noinline, + ..Func::default() + } }; self.functions.insert(local.clone(), local_function); names.insert(name, local); diff --git a/src/ast/lowering.rs b/src/ast/lowering.rs index 81dd730..1f83ce8 100644 --- a/src/ast/lowering.rs +++ b/src/ast/lowering.rs @@ -41,8 +41,8 @@ impl AstBuilderImpl<'_> { let operand = std::mem::take(node); *node = Node { nt: NodeType::Cast, - cty: Some(target.clone()), token: operand.token.clone(), + cty: Some(target.clone()), lhs: Some(Box::new(operand)), ..Node::default() }; @@ -88,8 +88,8 @@ impl AstBuilderImpl<'_> { } if matches!( node.nt, - NodeType::If - | NodeType::Conditional + NodeType::Conditional + | NodeType::If | NodeType::While | NodeType::DoWhile | NodeType::For diff --git a/src/ast/overflow.rs b/src/ast/overflow.rs index b4e71ad..fbad28d 100644 --- a/src/ast/overflow.rs +++ b/src/ast/overflow.rs @@ -13,9 +13,9 @@ fn number(value: usize, ty: Type, token: &Token) -> Node { fn cast(value: Node, ty: Type, token: &Token) -> Node { Node { nt: NodeType::Cast, - lhs: Some(Box::new(value)), - cty: Some(ty), token: Some(token.clone()), + cty: Some(ty), + lhs: Some(Box::new(value)), ..Node::default() } } @@ -26,9 +26,9 @@ fn temporary(ty: Type, offset: &mut usize, token: &Token) -> Node { *offset = (*offset + ty.size_of()).div_ceil(ty.align_of()) * ty.align_of(); Node { nt: NodeType::LocalVar, + token: Some(token.clone()), cty: Some(ty), offset: Some(*offset), - token: Some(token.clone()), ..Node::default() } } @@ -83,10 +83,10 @@ impl AstBuilderImpl<'_> { } Node { nt: operation, - cty: Some(Type::Bool), token: Some(token), - volatile_access: target.qualifiers().volatile, + cty: Some(Type::Bool), args: vec![lhs, rhs, pointer], + volatile_access: target.qualifiers().volatile, ..Node::default() } } diff --git a/src/ast/statement_expression.rs b/src/ast/statement_expression.rs index 8b144a4..e7e804b 100644 --- a/src/ast/statement_expression.rs +++ b/src/ast/statement_expression.rs @@ -16,8 +16,8 @@ impl AstBuilderImpl<'_> { let has_value = statements.last().is_some_and(|node| { !matches!( node.nt, - NodeType::Block - | NodeType::DefVar + NodeType::DefVar + | NodeType::Block | NodeType::Return | NodeType::If | NodeType::While @@ -87,9 +87,9 @@ impl AstBuilderImpl<'_> { fn validate_statement_expression_jumps(&self, node: &Node, loops: usize, switches: usize) { let escaping = match node.nt { NodeType::Return | NodeType::Goto | NodeType::Label => true, + NodeType::Case => switches == 0, NodeType::Break => loops == 0 && switches == 0, NodeType::Continue => loops == 0, - NodeType::Case => switches == 0, _ => false, }; if escaping { diff --git a/src/ast/transparent_union.rs b/src/ast/transparent_union.rs index ffc057d..0a4598a 100644 --- a/src/ast/transparent_union.rs +++ b/src/ast/transparent_union.rs @@ -95,8 +95,8 @@ impl AstBuilderImpl<'_> { if null && matches!(member.ty.unqualified(), Type::Ptr(_)) { value = Node { nt: NodeType::Cast, - cty: Some(member.ty.clone()), token: Some(token.clone()), + cty: Some(member.ty.clone()), lhs: Some(Box::new(value)), ..Node::default() }; diff --git a/src/ast/type_parser.rs b/src/ast/type_parser.rs index a9721d9..c6d662e 100644 --- a/src/ast/type_parser.rs +++ b/src/ast/type_parser.rs @@ -788,11 +788,11 @@ impl AstBuilderImpl<'_> { || format!(".anonymous.{}", members.len()), |name| name.s_value.to_string(), ), - anonymous, - requested_alignment, ty: member_type, offset, + requested_alignment, bit_field: (field.width != 0).then_some(field), + anonymous, }); } if self.attempt_reserved(",").is_none() { @@ -813,6 +813,7 @@ impl AstBuilderImpl<'_> { } *ty.definition().borrow_mut() = StructDefinition { members, + complete: true, is_union, transparent_union: false, has_bit_fields, @@ -821,7 +822,6 @@ impl AstBuilderImpl<'_> { align, natural_align, packing, - complete: true, }; } if defining { diff --git a/src/ast/validation.rs b/src/ast/validation.rs index 41c5313..14bb19c 100644 --- a/src/ast/validation.rs +++ b/src/ast/validation.rs @@ -11,17 +11,6 @@ use crate::{ impl AstBuilderImpl<'_> { pub(super) fn validate_type_layout(&self, ty: &Type, token: &Token) { match ty { - Type::Qualified(child, q) => { - if q.restricted - && !matches!(child.unqualified(), Type::Ptr(pointee) if pointee.function_signature_ref().is_none()) - { - self.error_logger.print_error_position( - token.pos, - "restrict requires a pointer to an object or void", - ); - } - self.validate_type_layout(child, token); - } Type::Ptr(child) => self.validate_type_layout(child, token), Type::Arr(child, _) | Type::Vla(child, _) => { if child.size_of() % child.align_of() != 0 { @@ -94,6 +83,17 @@ impl AstBuilderImpl<'_> { self.validate_type_layout(ty, token); } } + Type::Qualified(child, q) => { + if q.restricted + && !matches!(child.unqualified(), Type::Ptr(pointee) if pointee.function_signature_ref().is_none()) + { + self.error_logger.print_error_position( + token.pos, + "restrict requires a pointer to an object or void", + ); + } + self.validate_type_layout(child, token); + } _ => {} } if ty.checked_size_of().is_none() { @@ -121,6 +121,12 @@ impl AstBuilderImpl<'_> { let ty = ty.unqualified(); match (ty, data) { (_, GlobalVariableData::Zero) => {} + (Type::Integer128 { .. }, _) => { + self.error_logger.print_error_position( + token.pos, + "128-bit integer value initialization is not supported", + ); + } (Type::Float16, _) => { self.error_logger.print_error_position( token.pos, @@ -133,12 +139,18 @@ impl AstBuilderImpl<'_> { "extended long double value initialization is not supported", ); } - (Type::Integer128 { .. }, _) => { - self.error_logger.print_error_position( - token.pos, - "128-bit integer value initialization is not supported", - ); + (Type::Arr(child, size), GlobalVariableData::Arr(elements)) => { + if size.is_some_and(|size| elements.len() > size) { + self.error_logger + .print_error_position(token.pos, "too many array initializer elements"); + } + for element in elements { + self.validate_global_initializer(child, element, token); + } } + (Type::Arr(..), _) => self + .error_logger + .print_error_position(token.pos, "array initializer requires braces"), (Type::Struct(ty), GlobalVariableData::Union { index, value }) => { self.validate_global_initializer(&ty.member(*index).ty, value, token); } @@ -164,18 +176,6 @@ impl AstBuilderImpl<'_> { self.error_logger .print_error_position(token.pos, "global struct initializer requires braces"); } - (Type::Arr(child, size), GlobalVariableData::Arr(elements)) => { - if size.is_some_and(|size| elements.len() > size) { - self.error_logger - .print_error_position(token.pos, "too many array initializer elements"); - } - for element in elements { - self.validate_global_initializer(child, element, token); - } - } - (Type::Arr(..), _) => self - .error_logger - .print_error_position(token.pos, "array initializer requires braces"), (_, GlobalVariableData::Arr(elements)) => { if elements.len() != 1 { self.error_logger @@ -402,11 +402,11 @@ impl AstBuilderImpl<'_> { if matches!( node.nt, NodeType::Mod - | NodeType::BitAnd - | NodeType::BitOr - | NodeType::BitXor | NodeType::BitLeft | NodeType::BitRight + | NodeType::BitAnd + | NodeType::BitXor + | NodeType::BitOr | NodeType::BitNot ) { for operand in [&node.lhs, &node.rhs].into_iter().flatten() { @@ -457,24 +457,24 @@ impl AstBuilderImpl<'_> { } if matches!( node.nt, - NodeType::Assign - | NodeType::Add + NodeType::Add | NodeType::Sub | NodeType::Mul | NodeType::Div | NodeType::Mod - | NodeType::Eq - | NodeType::Ne - | NodeType::Lt - | NodeType::Le | NodeType::BitLeft | NodeType::BitRight | NodeType::BitAnd - | NodeType::BitOr | NodeType::BitXor + | NodeType::BitOr | NodeType::BitNot + | NodeType::Eq + | NodeType::Ne + | NodeType::Lt + | NodeType::Le | NodeType::LogicalAnd | NodeType::LogicalOr + | NodeType::Assign ) { for operand in [&node.lhs, &node.rhs].into_iter().flatten() { no_value(operand); diff --git a/src/ast/varargs.rs b/src/ast/varargs.rs index df962aa..6cfa007 100644 --- a/src/ast/varargs.rs +++ b/src/ast/varargs.rs @@ -7,8 +7,8 @@ fn number(value: usize) -> Node { fn cast(node: Node, ty: Type) -> Node { Node { nt: NodeType::Cast, - lhs: Some(Box::new(node)), cty: Some(ty), + lhs: Some(Box::new(node)), ..Node::default() } } @@ -97,20 +97,20 @@ impl AstBuilderImpl<'_> { let size = ty.size_of(); members.push(StructMember { name: name.into(), - anonymous: false, - requested_alignment: 0, ty, offset, + requested_alignment: 0, bit_field: None, + anonymous: false, }); offset += size; } *owner.borrow_mut() = StructDefinition { members, + complete: true, size: offset.div_ceil(alignment) * alignment, align: alignment, natural_align: alignment, - complete: true, ..StructDefinition::default() }; self.struct_definitions.push(owner); @@ -133,7 +133,7 @@ impl AstBuilderImpl<'_> { fn va_address(&self, ap: Node, token: &Token) -> Node { let ty = ap.resolve_type().unwrap(); let valid = if cfg!(target_arch = "x86_64") { - matches!(ty.unqualified(), Type::Arr(..) | Type::Ptr(_)) + matches!(ty.unqualified(), Type::Ptr(_) | Type::Arr(..)) } else if cfg!(target_os = "macos") { matches!(ty.unqualified(), Type::Ptr(_)) } else { @@ -171,14 +171,14 @@ impl AstBuilderImpl<'_> { } Node { nt: NodeType::VaStart, + token: Some(token), + cty: Some(Type::Void), lhs: Some(Box::new(ap)), rhs: Some(Box::new(Node { cty: self.return_type.clone(), ..Node::default() })), parameter_types: parameters, - cty: Some(Type::Void), - token: Some(token), ..Node::default() } } @@ -246,9 +246,9 @@ impl AstBuilderImpl<'_> { self.expect_reserved(")"); Node { nt: NodeType::VaEnd, - lhs: Some(Box::new(ap)), - cty: Some(Type::Void), token: Some(token), + cty: Some(Type::Void), + lhs: Some(Box::new(ap)), ..Node::default() } } diff --git a/src/ast/vla.rs b/src/ast/vla.rs index a1081f2..f99872d 100644 --- a/src/ast/vla.rs +++ b/src/ast/vla.rs @@ -5,15 +5,15 @@ fn cast(node: Node, ty: Type) -> Node { Node { nt: NodeType::Cast, token: node.token.clone(), - lhs: Some(Box::new(node)), cty: Some(ty), + lhs: Some(Box::new(node)), ..Node::default() } } fn number(value: usize) -> Node { Node { - value: Some(value), cty: Some(Type::integer(64, false)), + value: Some(value), ..Node::default() } } @@ -95,9 +95,9 @@ impl AstBuilderImpl<'_> { pub(super) fn dynamic_node(ty: Type, offset: usize, token: Token) -> Node { let pointer = Node { nt: NodeType::LocalVar, + token: Some(token.clone()), cty: Some(Type::Ptr(Rc::new(ty))), offset: Some(offset), - token: Some(token.clone()), ..Node::default() }; Node::new_with_op_and_lhs(Some(token), NodeType::Deref, pointer) @@ -113,30 +113,30 @@ impl AstBuilderImpl<'_> { } pub(super) fn runtime_size(ty: &Type) -> Node { match ty.unqualified() { + Type::Arr(child, size) if child.is_variable_array() => binary( + NodeType::Mul, + number(size.unwrap_or(0)), + Self::runtime_size(child), + ), Type::Vla(child, offset) => binary( NodeType::Mul, Node { nt: NodeType::LocalVar, - offset: Some(*offset), cty: Some(Type::integer(64, false)), + offset: Some(*offset), ..Node::default() }, Self::runtime_size(child), ), - Type::Arr(child, size) if child.is_variable_array() => binary( - NodeType::Mul, - number(size.unwrap_or(0)), - Self::runtime_size(child), - ), _ => number(ty.size_of()), } } fn allocation_call(name: &str, value: Node, result: Type, parameter: Type) -> Node { Node { nt: NodeType::CallFunc, + cty: Some(result), global_name: name.into(), args: vec![value], - cty: Some(result), parameter_types: vec![parameter], ..Node::default() } diff --git a/src/bin/dynamite-cc/arguments.rs b/src/bin/dynamite-cc/arguments.rs index d14e737..6f1ea0d 100644 --- a/src/bin/dynamite-cc/arguments.rs +++ b/src/bin/dynamite-cc/arguments.rs @@ -16,20 +16,20 @@ fn argument_value(args: &[String], index: &mut usize) -> Result impl Arguments { pub(super) fn parse(args: &[String]) -> Result { let mut result = Self { + mode: Mode::Link, + preprocess_only: false, + emit_llvm: false, + jobs: 1, inputs: vec![], + output: None, + preprocessing: vec![], backend: vec![], linker: vec![], - preprocessing: vec![], forwarded: vec![], - output: None, - mode: Mode::Link, - jobs: 1, dependencies: None, dependency_file: None, dependency_targets: vec![], phony_dependencies: false, - preprocess_only: false, - emit_llvm: false, }; let preprocess_only = args .iter() diff --git a/src/bitfield.rs b/src/bitfield.rs index f3760d2..972c2a7 100644 --- a/src/bitfield.rs +++ b/src/bitfield.rs @@ -26,9 +26,9 @@ fn temporary(ty: Type, offset: &mut usize, source: &Node) -> Node { *offset = (*offset + ty.size_of()).div_ceil(ty.align_of()) * ty.align_of(); Node { nt: NodeType::LocalVar, + token: source.token.clone(), cty: Some(ty), offset: Some(*offset), - token: source.token.clone(), ..Node::default() } } diff --git a/src/ctype.rs b/src/ctype.rs index 7ab7735..3084f0f 100644 --- a/src/ctype.rs +++ b/src/ctype.rs @@ -201,9 +201,9 @@ impl Type { (Self::Enum(value), other) | (other, Self::Enum(value)) => { value.underlying.borrow().compatible(other) } - (Self::Vla(left, _), Self::Vla(right, _) | Self::Arr(right, _)) + (Self::Ptr(left), Self::Ptr(right)) | (Self::Arr(left, _), Self::Vla(right, _)) - | (Self::Ptr(left), Self::Ptr(right)) => { + | (Self::Vla(left, _), Self::Arr(right, _) | Self::Vla(right, _)) => { left.qualifiers() == right.qualifiers() && left.compatible(right) } (Self::Arr(left, count), Self::Arr(right, other_count)) => { @@ -315,23 +315,23 @@ impl Type { pub fn has_flexible_array(&self) -> bool { match self.unqualified() { - Self::Struct(ty) => ty.definition().borrow().has_flexible_array, Self::Arr(child, _) => child.has_flexible_array(), + Self::Struct(ty) => ty.definition().borrow().has_flexible_array, _ => false, } } pub fn is_variable_array(&self) -> bool { match self.unqualified() { - Self::Vla(..) => true, Self::Arr(child, _) => child.is_variable_array(), + Self::Vla(..) => true, _ => false, } } pub fn is_variably_modified(&self) -> bool { match self.unqualified() { + Self::Ptr(child) | Self::Arr(child, _) => child.is_variably_modified(), Self::Vla(..) => true, - Self::Arr(child, _) | Self::Ptr(child) => child.is_variably_modified(), _ => false, } } @@ -415,12 +415,12 @@ impl Type { } pub fn integer_info(&self) -> Option<(u8, bool)> { match self { - Self::Enum(value) => value.underlying.borrow().integer_info(), + Self::Bool => Some((8, false)), Self::I8 | Self::SignedChar => Some((8, true)), - Self::LongLong { signed } => Some((64, *signed)), Self::I32 => Some((32, true)), Self::Integer { bits, signed } => Some((*bits, *signed)), - Self::Bool => Some((8, false)), + Self::LongLong { signed } => Some((64, *signed)), + Self::Enum(value) => value.underlying.borrow().integer_info(), Self::Qualified(child, _) | Self::Aligned(child, _) => child.integer_info(), _ => None, } @@ -496,7 +496,6 @@ impl Type { } pub fn qualifiers(&self) -> Qualifiers { match self { - Self::Aligned(child, _) => child.qualifiers(), Self::Qualified(child, q) => { let inherited = child.qualifiers(); Qualifiers { @@ -505,6 +504,7 @@ impl Type { restricted: q.restricted || inherited.restricted, } } + Self::Aligned(child, _) => child.qualifiers(), _ => Qualifiers::default(), } } @@ -529,15 +529,15 @@ impl Type { pub(crate) fn lower_for_codegen(&mut self) { // Scalar annotations need no recursive normalization. match self { - Self::Qualified(..) - | Self::Aligned(..) - | Self::Vector(..) + Self::Enum(..) | Self::Ptr(..) | Self::Arr(..) | Self::Vla(..) + | Self::Vector(..) | Self::Func(..) | Self::VariadicFunc(..) - | Self::Enum(..) => self.lower_compound_for_codegen(), + | Self::Qualified(..) + | Self::Aligned(..) => self.lower_compound_for_codegen(), _ => {} } } @@ -557,11 +557,11 @@ impl Type { *self = underlying; self.lower_for_codegen(); } - Self::Aligned(child, _) - | Self::Vector(child, _) - | Self::Ptr(child) + Self::Ptr(child) | Self::Arr(child, _) - | Self::Vla(child, _) => Rc::make_mut(child).lower_for_codegen(), + | Self::Vla(child, _) + | Self::Vector(child, _) + | Self::Aligned(child, _) => Rc::make_mut(child).lower_for_codegen(), Self::Func(args, result) | Self::VariadicFunc(args, result) => { for arg in Rc::make_mut(args) { arg.lower_parameter_for_codegen(); @@ -588,9 +588,6 @@ impl Type { /// Reuse an owned signature's storage when constructing a call. pub(crate) fn into_function_signature(self) -> Option<(Vec, Type, bool)> { match self { - Self::Qualified(child, _) | Self::Aligned(child, _) => { - Rc::unwrap_or_clone(child).into_function_signature() - } Self::Func(args, result) => Some(( Rc::unwrap_or_clone(args), Rc::unwrap_or_clone(result), @@ -599,6 +596,9 @@ impl Type { Self::VariadicFunc(args, result) => { Some((Rc::unwrap_or_clone(args), Rc::unwrap_or_clone(result), true)) } + Self::Qualified(child, _) | Self::Aligned(child, _) => { + Rc::unwrap_or_clone(child).into_function_signature() + } _ => None, } } @@ -623,24 +623,24 @@ impl Type { pub fn size_of(&self) -> usize { match self { - Type::Qualified(child, _) | Type::Aligned(child, _) => child.size_of(), - Type::Enum(value) => value.underlying.borrow().size_of(), Type::Void => 0, - Type::Struct(ty) => ty.definition().borrow().size, + Type::Bool => 1, Type::I8 | Type::SignedChar => 1, - Type::LongLong { .. } => 8, Type::I32 => 4, - Type::Float16 => 2, - Type::Vector(element, lanes) => element.size_of() * lanes, - Type::Integer128 { .. } => 16, Type::Integer { bits, .. } => (*bits as usize) / 8, - Type::Bool => 1, + Type::LongLong { .. } => 8, + Type::Integer128 { .. } => 16, + Type::Enum(value) => value.underlying.borrow().size_of(), + Type::Float16 => 2, Type::LongDouble(80) => 16, Type::Float(bits) | Type::LongDouble(bits) => usize::from(*bits) / 8, Type::Ptr(_) => 8, Type::Arr(t, s) => t.size_of() * s.unwrap_or(0), Type::Vla(..) => 8, + Type::Vector(element, lanes) => element.size_of() * lanes, + Type::Struct(ty) => ty.definition().borrow().size, Type::Func(..) | Type::VariadicFunc(..) => 1, + Type::Qualified(child, _) | Type::Aligned(child, _) => child.size_of(), } } pub(crate) fn contains_volatile_subobject(&self) -> bool { @@ -660,8 +660,8 @@ impl Type { pub fn contains_vector(&self) -> bool { match self.unqualified() { - Self::Vector(..) => true, Self::Arr(child, _) | Self::Vla(child, _) => child.contains_vector(), + Self::Vector(..) => true, Self::Struct(record) => record .definition() .borrow() @@ -703,10 +703,7 @@ impl Type { pub fn contains_typedef_alignment(&self) -> bool { match self { - Self::Aligned(child, alignment) => { - *alignment != child.natural_align_of() || child.contains_typedef_alignment() - } - Self::Qualified(child, _) | Self::Arr(child, _) | Self::Vla(child, _) => { + Self::Arr(child, _) | Self::Vla(child, _) | Self::Qualified(child, _) => { child.contains_typedef_alignment() } Self::Struct(record) => record @@ -715,12 +712,15 @@ impl Type { .members .iter() .any(|member| member.ty.contains_typedef_alignment()), + Self::Aligned(child, alignment) => { + *alignment != child.natural_align_of() || child.contains_typedef_alignment() + } _ => false, } } pub fn natural_align_of(&self) -> usize { - if let Self::Aligned(child, _) | Self::Qualified(child, _) = self { + if let Self::Qualified(child, _) | Self::Aligned(child, _) = self { return child.natural_align_of(); } if let Self::Struct(ty) = self.unqualified() { @@ -735,11 +735,11 @@ impl Type { pub fn align_of(&self) -> usize { match self { - Self::Aligned(_, alignment) => *alignment, - Self::Qualified(child, _) => child.align_of(), Self::Void => 1, - Self::Struct(ty) => ty.definition().borrow().align.max(1), Self::Arr(child, _) | Self::Vla(child, _) => child.align_of(), + Self::Struct(ty) => ty.definition().borrow().align.max(1), + Self::Qualified(child, _) => child.align_of(), + Self::Aligned(_, alignment) => *alignment, _ => self.size_of(), } } diff --git a/src/generator/aarch64/generator.rs b/src/generator/aarch64/generator.rs index 09dbd62..be96886 100644 --- a/src/generator/aarch64/generator.rs +++ b/src/generator/aarch64/generator.rs @@ -63,7 +63,6 @@ impl<'a> AsmGenerator<'a> { let generator = Self { error_logger: self.error_logger, target_os: self.target_os, - symbol_names: ast.symbol_names.clone(), defined_symbols: ast .functions .iter() @@ -76,6 +75,7 @@ impl<'a> AsmGenerator<'a> { .map(|(name, _)| name.clone()), ) .collect(), + symbol_names: ast.symbol_names.clone(), }; let references = if ast.functions.values().any(|function| { @@ -615,128 +615,45 @@ impl<'a> AsmGenerator<'a> { fn gen_node_inner(&self, node: &Node, options: Options) -> Assembly { match node.nt { - NodeType::Prefetch(write, locality) => { - return self.gen_prefetch(node, write, locality, options); - } - NodeType::InlineAsm(_, _) => return self.gen_inline_assembly(node, options), - NodeType::Atomic(operation) => { - return self.gen_atomic(node, operation, options); - } - NodeType::VaStart | NodeType::VaEnd => { - let mut lines = vec![ - self.gen_node(node.lhs.as_ref().unwrap(), options), - Self::pop(X11), - ]; - if node.nt == NodeType::VaStart { - let (gp, fp, stack) = crate::generator::abi::variadic_state( - node, - crate::generator::Arch::Aarch64, - self.target_os, - ); - lines.extend([ - Assembly::inst3(ADD, X8, X29, options.offset + 32 + stack), - Assembly::inst2(STR, X8, Ptr(X11, 8)), - ]); - if !matches!(self.target_os, Os::MacOS) { - for (position, top) in [(8, 64), (16, 192)] { - lines.extend([ - Assembly::inst3(ADD, X8, X29, top), - Assembly::inst3(ADD, X10, X11, position), - Assembly::inst2(STR, X8, Ptr(X10, 8)), - ]); - } - for (position, remaining) in [(24, (8 - gp) * 8), (28, (8 - fp) * 16)] { - lines.extend([ - Assembly::inst2(MOV, X8, 0), - Assembly::inst3(SUB, X8, X8, remaining), - Assembly::inst3(ADD, X10, X11, position), - Assembly::inst2(STR, X8, Ptr(X10, 4)), - ]); - } - } - } - lines.push(Self::push(0)); - return lines.into(); - } - - NodeType::CopyBytes { bytes } => { - let mut lines = vec![ - self.gen_addr(node.lhs.as_ref().unwrap(), options), - self.gen_addr(node.rhs.as_ref().unwrap(), options), - Self::pop(X10), - Self::pop(X11), - Self::push(X11), - ]; - if bytes >= 8 { - lines.extend([ - Assembly::inst2(MOV, X9, bytes / 8), - "902:".into(), - Assembly::inst2(LDR, X12, Ptr(X10, 8)), - Assembly::inst2(STR, X12, Ptr(X11, 8)), - Assembly::inst3(ADD, X10, X10, 8), - Assembly::inst3(ADD, X11, X11, 8), - Assembly::inst3(SUB, X9, X9, 1), - Assembly::inst2(CMP, X9, 0), - Assembly::inst1(JNE, "902b"), - ]); - } - for _ in 0..bytes % 8 { - lines.extend([ - Assembly::inst2(LDRU, X12, Ptr(X10, 1)), - Assembly::inst2(STR, X12, Ptr(X11, 1)), - Assembly::inst3(ADD, X10, X10, 1), - Assembly::inst3(ADD, X11, X11, 1), - ]); - } - return lines.into(); + NodeType::Num => { + return Self::push(node.value.unwrap()); } - NodeType::ZeroFill { bytes } => { - let mut lines = vec![ - self.gen_addr(node.lhs.as_ref().unwrap(), options), - Self::pop(X11), - Assembly::inst2(MOV, X9, 0), - ]; - if bytes >= 8 { - lines.extend([ - Assembly::inst2(MOV, X10, bytes / 8), - // Numeric labels stay local to this closed instruction sequence. - "901:".into(), - Assembly::inst2(STR, X9, Ptr(X11, 8)), - Assembly::inst3(ADD, X11, X11, 8), - Assembly::inst3(SUB, X10, X10, 1), - Assembly::inst2(CMP, X10, 0), - Assembly::inst1(JNE, "901b"), - ]); - } - for _ in 0..bytes % 8 { - lines.extend([ - Assembly::inst2(STR, X9, Ptr(X11, 1)), - Assembly::inst3(ADD, X11, X11, 1), - ]); - } - lines.push(Self::push(0)); - return lines.into(); + NodeType::LocalVar | NodeType::GlobalVar => { + return vec![ + self.gen_addr(node, options), + if let Some( + Type::Arr(..) + | Type::Vla(..) + | Type::Struct(_) + | Type::Func(..) + | Type::VariadicFunc(..), + ) = node.resolve_type() + { + vec![].into() + } else { + vec![Self::pop(X8), self.deref(node), Self::push(X8)].into() + }, + ] + .into(); } - NodeType::BitScan { bits, leading } => { - let register = if bits == 32 { W8 } else { X8 }; - let mut lines = vec![ - self.gen_node(node.lhs.as_ref().unwrap(), options), - Self::pop(X8), - ]; - if !leading { - lines.push(Assembly::inst2(RBIT, register, register)); - } - lines.extend([Assembly::inst2(CLZ, register, register), Self::push(X8)]); - return lines.into(); + NodeType::Addr => { + return self.gen_addr(node.lhs.as_ref().unwrap(), options); } - NodeType::FloatAbs => { - let shift = 65 - node.resolve_type().unwrap().float_bits().unwrap() as usize; + NodeType::Deref => { return vec![ self.gen_node(node.lhs.as_ref().unwrap(), options), - Self::pop(X8), - Assembly::inst3(SHL, X8, X8, shift), - Assembly::inst3(SHR, X8, X8, shift), - Self::push(X8), + if let Some( + Type::Arr(..) + | Type::Vla(..) + | Type::Struct(_) + | Type::Func(..) + | Type::VariadicFunc(..), + ) = node.lhs.as_ref().unwrap().dest_type() + { + vec![].into() + } else { + vec![Self::pop(X8), self.deref(node), Self::push(X8)].into() + }, ] .into(); } @@ -758,65 +675,169 @@ impl<'a> AsmGenerator<'a> { lines.push(Self::push(X8)); return lines.into(); } - NodeType::Sequence => { - let mut lines = vec![]; - for (index, child) in node.children.iter().enumerate() { - lines.push(self.gen_node(child, options)); - if index + 1 < node.children.len() - && !matches!(child.nt, NodeType::Block | NodeType::DefVar) - { - lines.push(Self::pop(X8)); - } - } - return lines.into(); + NodeType::BitLeft | NodeType::BitRight => { + return vec![ + self.gen_node(node.rhs.as_ref().unwrap(), options), + self.gen_node(node.lhs.as_ref().unwrap(), options), + Self::pop(X8), + Self::pop(X10), + Assembly::inst3( + if matches!(node.nt, NodeType::BitLeft) { + SHL + } else if node.resolve_type().unwrap().integer_info().unwrap().1 { + SAR + } else { + SHR + }, + X8, + X8, + X10, + ), + Self::normalize_integer(&node.resolve_type().unwrap(), X8), + Self::push(X8), + ] + .into(); } - NodeType::DefVar => { - return self.gen_statements(&node.children, options); + NodeType::BitNot => { + return vec![ + self.gen_node(node.lhs.as_ref().unwrap(), options), + Self::pop(X8), + Assembly::inst2(MVN, X8, X8), + Self::normalize_integer(&node.resolve_type().unwrap(), X8), + Self::push(X8), + ] + .into(); } - NodeType::CallFunc => { - if matches!(node.resolve_type(), Some(Type::Struct(_))) - || node - .args - .iter() - .any(|arg| matches!(arg.resolve_type(), Some(Type::Struct(_)))) - { - return self.aggregate_call(node, options); - } - if node.resolve_type().unwrap().float_bits().is_some() - || node - .args - .iter() - .any(|arg| arg.resolve_type().unwrap().float_bits().is_some()) - { - return self.float_call(node, options); - } - let fixed = if matches!(self.target_os, Os::MacOS) && node.variadic { - node.parameter_types.len() - } else { - node.args.len() + NodeType::Conditional | NodeType::If => { + let branch_num = node.token.as_ref().unwrap().pos; + let branch = |child: &Node| { + if node.nt == NodeType::If { + self.gen_statements(std::slice::from_ref(child), options) + } else { + self.gen_node(child, options) + } }; - let types = node - .args - .iter() - .rev() - .enumerate() - .map(|(i, arg)| { - node.parameter_types - .get(i) - .cloned() - .unwrap_or_else(|| arg.resolve_type().unwrap()) - }) - .collect::>(); - let (stack_slots, stack_size) = self.stack_arguments(&types, fixed); - let mut lines = vec![ - node.lhs - .as_ref() - .map(|callee| self.gen_node(callee, options)) + return vec![ + self.gen_node(node.cond.as_ref().unwrap(), options), + Self::pop(X8), + Assembly::inst2(CMP, X8, 0), + Assembly::inst1(JE, ElseFlag(branch_num)), + branch(node.then.as_deref().unwrap()), + Assembly::inst1(JMP, EndFlag(branch_num)), + format!("{}:", ElseFlag(branch_num)).into(), + node.els + .as_deref() + .map(branch) .unwrap_or_else(|| vec![].into()), - node.args - .iter() - .map(|arg| self.gen_node(arg, options)) - .collect::>() + format!("{}:", EndFlag(branch_num)).into(), + if matches!(node.nt, NodeType::Conditional) + && node + .resolve_type() + .is_some_and(|ty| ty.integer_info().is_some()) + { + vec![ + Self::pop(X8), + Self::normalize_integer(&node.resolve_type().unwrap(), X8), + Self::push(X8), + ] + .into() + } else { + vec![].into() + }, + ] + .into(); + } + NodeType::Assign => { + return vec![ + self.gen_addr(node.lhs.as_ref().unwrap(), options), + self.gen_node(node.rhs.as_ref().unwrap(), options), + Self::pop(X13), + Self::pop(X8), + Assembly::inst2( + STR, + X13, + Ptr( + X8, + node.lhs.as_ref().unwrap().resolve_type().unwrap().size_of(), + ), + ), + self.deref(node.lhs.as_ref().unwrap()), + Self::push(X8), + ] + .into(); + } + NodeType::SuffixIncr | NodeType::SuffixDecr => { + let op = if let NodeType::SuffixIncr = node.nt { + ADD + } else { + SUB + }; + return vec![ + self.gen_addr(node.lhs.as_ref().unwrap(), options), + Self::pop(X8), + Assembly::inst2(MOV, X13, 1), + if let Some(t) = node.lhs.as_ref().unwrap().dest_type() { + vec![ + Assembly::inst2(MOV, X10, t.size_of()), + Assembly::inst3(MUL, X13, X13, X10), + ] + .into() + } else { + vec![].into() + }, + Assembly::inst2(MOV, X11, X8), + self.deref(node.lhs.as_ref().unwrap()), + Assembly::inst3(op, X13, X8, X13), + Self::normalize_integer(&node.resolve_type().unwrap(), X13), + Assembly::inst2(STR, X13, Ptr(X11, node.resolve_type().unwrap().size_of())), + Self::push(X8), + ] + .into(); + } + NodeType::CallFunc => { + if matches!(node.resolve_type(), Some(Type::Struct(_))) + || node + .args + .iter() + .any(|arg| matches!(arg.resolve_type(), Some(Type::Struct(_)))) + { + return self.aggregate_call(node, options); + } + if node.resolve_type().unwrap().float_bits().is_some() + || node + .args + .iter() + .any(|arg| arg.resolve_type().unwrap().float_bits().is_some()) + { + return self.float_call(node, options); + } + let fixed = if matches!(self.target_os, Os::MacOS) && node.variadic { + node.parameter_types.len() + } else { + node.args.len() + }; + let types = node + .args + .iter() + .rev() + .enumerate() + .map(|(i, arg)| { + node.parameter_types + .get(i) + .cloned() + .unwrap_or_else(|| arg.resolve_type().unwrap()) + }) + .collect::>(); + let (stack_slots, stack_size) = self.stack_arguments(&types, fixed); + let mut lines = vec![ + node.lhs + .as_ref() + .map(|callee| self.gen_node(callee, options)) + .unwrap_or_else(|| vec![].into()), + node.args + .iter() + .map(|arg| self.gen_node(arg, options)) + .collect::>() .into(), ]; lines.push(Assembly::inst3(SUB, X10, X19, stack_size)); @@ -863,95 +884,64 @@ impl<'a> AsmGenerator<'a> { ]); return lines.into(); } - NodeType::If | NodeType::Conditional => { - let branch_num = node.token.as_ref().unwrap().pos; - let branch = |child: &Node| { - if node.nt == NodeType::If { - self.gen_statements(std::slice::from_ref(child), options) - } else { - self.gen_node(child, options) + NodeType::Sequence => { + let mut lines = vec![]; + for (index, child) in node.children.iter().enumerate() { + lines.push(self.gen_node(child, options)); + if index + 1 < node.children.len() + && !matches!(child.nt, NodeType::DefVar | NodeType::Block) + { + lines.push(Self::pop(X8)); } - }; + } + return lines.into(); + } + NodeType::DefVar => { + return self.gen_statements(&node.children, options); + } + NodeType::Block => { + return self.gen_statements(&node.children, options); + } + NodeType::Return => { + if matches!(node.resolve_type(), Some(Type::Struct(_))) { + return self.aggregate_return(node, options); + } + if node.lhs.is_none() { + return Self::epilogue(options.offset); + } return vec![ - self.gen_node(node.cond.as_ref().unwrap(), options), - Self::pop(X8), - Assembly::inst2(CMP, X8, 0), - Assembly::inst1(JE, ElseFlag(branch_num)), - branch(node.then.as_deref().unwrap()), - Assembly::inst1(JMP, EndFlag(branch_num)), - format!("{}:", ElseFlag(branch_num)).into(), - node.els - .as_deref() - .map(branch) - .unwrap_or_else(|| vec![].into()), - format!("{}:", EndFlag(branch_num)).into(), - if matches!(node.nt, NodeType::Conditional) - && node - .resolve_type() - .is_some_and(|ty| ty.integer_info().is_some()) - { - vec![ - Self::pop(X8), - Self::normalize_integer(&node.resolve_type().unwrap(), X8), - Self::push(X8), - ] - .into() + self.gen_node(node.lhs.as_ref().unwrap(), options), + Self::pop(X0), + if let Some(bits) = node.resolve_type().and_then(|ty| ty.float_bits()) { + Assembly::inst2(FMOV, Float(0, bits), if bits == 32 { W0 } else { X0 }) } else { vec![].into() }, + Self::epilogue(options.offset), ] .into(); } - NodeType::Switch => { + NodeType::While => { let branch_num = node.token.as_ref().unwrap().pos; - let mut lines = vec![ + let options = Options { + breakable_branch_num: branch_num, + ..options + }; + let v = vec![ + format!(".Lcontinue.{}:", branch_num).into(), + format!("{}:", BeginFlag(branch_num)).into(), self.gen_node(node.cond.as_ref().unwrap(), options), Self::pop(X8), - ]; - let mut default = format!("{}", EndFlag(branch_num)); - for case in &node.children { - let label = format!(".Lcase.{}", case.token.as_ref().unwrap().pos); - if let Some(value) = case.value { - lines.push(Assembly::inst2(MOV, X9, value)); - lines.push(Assembly::inst2(CMP, X8, X9)); - lines.push(Assembly::inst1(JE, label)); - } else { - default = label; - } - } - lines.push(Assembly::inst1(JMP, default)); - lines.push(self.gen_node( - node.then.as_ref().unwrap(), - Options { - breakable_branch_num: branch_num, - ..options - }, - )); - lines.push(format!("{}:", EndFlag(branch_num)).into()); - return lines.into(); - } - NodeType::Goto => { - return Assembly::inst1(JMP, format!(".Lgoto.{}", node.value.unwrap())); - } - NodeType::Label => { - return vec![ - format!(".Lgoto.{}:", node.value.unwrap()).into(), - self.gen_statements( - std::slice::from_ref(node.lhs.as_deref().unwrap()), - options, - ), - ] - .into(); - } - NodeType::Case => { - return vec![ - format!(".Lcase.{}:", node.token.as_ref().unwrap().pos).into(), + Assembly::inst2(CMP, X8, 0), + Assembly::inst1(JE, EndFlag(branch_num)), self.gen_statements( - std::slice::from_ref(node.lhs.as_deref().unwrap()), + std::slice::from_ref(node.then.as_deref().unwrap()), options, ), - ] - .into(); + Assembly::inst1(JMP, BeginFlag(branch_num)), + format!("{}:", EndFlag(branch_num)).into(), + ]; + return v.into(); } NodeType::DoWhile => { let branch_num = node.token.as_ref().unwrap().pos; @@ -974,28 +964,6 @@ impl<'a> AsmGenerator<'a> { ] .into(); } - NodeType::While => { - let branch_num = node.token.as_ref().unwrap().pos; - let options = Options { - breakable_branch_num: branch_num, - ..options - }; - let v = vec![ - format!(".Lcontinue.{}:", branch_num).into(), - format!("{}:", BeginFlag(branch_num)).into(), - self.gen_node(node.cond.as_ref().unwrap(), options), - Self::pop(X8), - Assembly::inst2(CMP, X8, 0), - Assembly::inst1(JE, EndFlag(branch_num)), - self.gen_statements( - std::slice::from_ref(node.then.as_deref().unwrap()), - options, - ), - Assembly::inst1(JMP, BeginFlag(branch_num)), - format!("{}:", EndFlag(branch_num)).into(), - ]; - return v.into(); - } NodeType::For => { let branch_num = node.token.as_ref().unwrap().pos; let options = Options { @@ -1027,162 +995,193 @@ impl<'a> AsmGenerator<'a> { ]; return v.into(); } - NodeType::Block => { - return self.gen_statements(&node.children, options); - } - NodeType::Continue => { - return Assembly::inst1(JMP, format!(".Lcontinue.{}", node.value.unwrap())); - } - NodeType::Break => { - if options.breakable_branch_num != 0 { - return Assembly::inst1(JMP, EndFlag(options.breakable_branch_num)); - } else { - self.error_logger.print_error_position( - node.token.as_ref().unwrap().pos, - "unexpected break found", - ); - } - } - NodeType::Return => { - if matches!(node.resolve_type(), Some(Type::Struct(_))) { - return self.aggregate_return(node, options); - } - if node.lhs.is_none() { - return Self::epilogue(options.offset); - } - return vec![ - self.gen_node(node.lhs.as_ref().unwrap(), options), - Self::pop(X0), - if let Some(bits) = node.resolve_type().and_then(|ty| ty.float_bits()) { - Assembly::inst2(FMOV, Float(0, bits), if bits == 32 { W0 } else { X0 }) - } else { - vec![].into() - }, - Self::epilogue(options.offset), - ] - .into(); - } - NodeType::Num => { - return Self::push(node.value.unwrap()); - } - NodeType::LocalVar | NodeType::GlobalVar => { - return vec![ - self.gen_addr(node, options), - if let Some( - Type::Arr(..) - | Type::Vla(..) - | Type::Struct(_) - | Type::Func(..) - | Type::VariadicFunc(..), - ) = node.resolve_type() - { - vec![].into() + NodeType::Switch => { + let branch_num = node.token.as_ref().unwrap().pos; + let mut lines = vec![ + self.gen_node(node.cond.as_ref().unwrap(), options), + Self::pop(X8), + ]; + let mut default = format!("{}", EndFlag(branch_num)); + for case in &node.children { + let label = format!(".Lcase.{}", case.token.as_ref().unwrap().pos); + if let Some(value) = case.value { + lines.push(Assembly::inst2(MOV, X9, value)); + lines.push(Assembly::inst2(CMP, X8, X9)); + lines.push(Assembly::inst1(JE, label)); } else { - vec![Self::pop(X8), self.deref(node), Self::push(X8)].into() + default = label; + } + } + lines.push(Assembly::inst1(JMP, default)); + lines.push(self.gen_node( + node.then.as_ref().unwrap(), + Options { + breakable_branch_num: branch_num, + ..options }, - ] - .into(); - } - NodeType::Addr => { - return self.gen_addr(node.lhs.as_ref().unwrap(), options); + )); + lines.push(format!("{}:", EndFlag(branch_num)).into()); + return lines.into(); } - NodeType::Deref => { + NodeType::Case => { return vec![ - self.gen_node(node.lhs.as_ref().unwrap(), options), - if let Some( - Type::Arr(..) - | Type::Vla(..) - | Type::Struct(_) - | Type::Func(..) - | Type::VariadicFunc(..), - ) = node.lhs.as_ref().unwrap().dest_type() - { - vec![].into() - } else { - vec![Self::pop(X8), self.deref(node), Self::push(X8)].into() - }, + format!(".Lcase.{}:", node.token.as_ref().unwrap().pos).into(), + self.gen_statements( + std::slice::from_ref(node.lhs.as_deref().unwrap()), + options, + ), ] .into(); } - NodeType::Assign => { + NodeType::Break => { + if options.breakable_branch_num != 0 { + return Assembly::inst1(JMP, EndFlag(options.breakable_branch_num)); + } else { + self.error_logger.print_error_position( + node.token.as_ref().unwrap().pos, + "unexpected break found", + ); + } + } + NodeType::Continue => { + return Assembly::inst1(JMP, format!(".Lcontinue.{}", node.value.unwrap())); + } + NodeType::Goto => { + return Assembly::inst1(JMP, format!(".Lgoto.{}", node.value.unwrap())); + } + NodeType::Label => { return vec![ - self.gen_addr(node.lhs.as_ref().unwrap(), options), - self.gen_node(node.rhs.as_ref().unwrap(), options), - Self::pop(X13), - Self::pop(X8), - Assembly::inst2( - STR, - X13, - Ptr( - X8, - node.lhs.as_ref().unwrap().resolve_type().unwrap().size_of(), - ), + format!(".Lgoto.{}:", node.value.unwrap()).into(), + self.gen_statements( + std::slice::from_ref(node.lhs.as_deref().unwrap()), + options, ), - self.deref(node.lhs.as_ref().unwrap()), - Self::push(X8), ] .into(); } - NodeType::BitLeft | NodeType::BitRight => { + NodeType::FloatAbs => { + let shift = 65 - node.resolve_type().unwrap().float_bits().unwrap() as usize; return vec![ - self.gen_node(node.rhs.as_ref().unwrap(), options), self.gen_node(node.lhs.as_ref().unwrap(), options), Self::pop(X8), - Self::pop(X10), - Assembly::inst3( - if matches!(node.nt, NodeType::BitLeft) { - SHL - } else if node.resolve_type().unwrap().integer_info().unwrap().1 { - SAR - } else { - SHR - }, - X8, - X8, - X10, - ), - Self::normalize_integer(&node.resolve_type().unwrap(), X8), + Assembly::inst3(SHL, X8, X8, shift), + Assembly::inst3(SHR, X8, X8, shift), Self::push(X8), ] .into(); } - NodeType::BitNot => { - return vec![ + NodeType::BitScan { bits, leading } => { + let register = if bits == 32 { W8 } else { X8 }; + let mut lines = vec![ self.gen_node(node.lhs.as_ref().unwrap(), options), Self::pop(X8), - Assembly::inst2(MVN, X8, X8), - Self::normalize_integer(&node.resolve_type().unwrap(), X8), - Self::push(X8), - ] - .into(); + ]; + if !leading { + lines.push(Assembly::inst2(RBIT, register, register)); + } + lines.extend([Assembly::inst2(CLZ, register, register), Self::push(X8)]); + return lines.into(); } - NodeType::SuffixIncr | NodeType::SuffixDecr => { - let op = if let NodeType::SuffixIncr = node.nt { - ADD - } else { - SUB - }; - return vec![ + NodeType::VaStart | NodeType::VaEnd => { + let mut lines = vec![ + self.gen_node(node.lhs.as_ref().unwrap(), options), + Self::pop(X11), + ]; + if node.nt == NodeType::VaStart { + let (gp, fp, stack) = crate::generator::abi::variadic_state( + node, + crate::generator::Arch::Aarch64, + self.target_os, + ); + lines.extend([ + Assembly::inst3(ADD, X8, X29, options.offset + 32 + stack), + Assembly::inst2(STR, X8, Ptr(X11, 8)), + ]); + if !matches!(self.target_os, Os::MacOS) { + for (position, top) in [(8, 64), (16, 192)] { + lines.extend([ + Assembly::inst3(ADD, X8, X29, top), + Assembly::inst3(ADD, X10, X11, position), + Assembly::inst2(STR, X8, Ptr(X10, 8)), + ]); + } + for (position, remaining) in [(24, (8 - gp) * 8), (28, (8 - fp) * 16)] { + lines.extend([ + Assembly::inst2(MOV, X8, 0), + Assembly::inst3(SUB, X8, X8, remaining), + Assembly::inst3(ADD, X10, X11, position), + Assembly::inst2(STR, X8, Ptr(X10, 4)), + ]); + } + } + } + lines.push(Self::push(0)); + return lines.into(); + } + NodeType::Prefetch(write, locality) => { + return self.gen_prefetch(node, write, locality, options); + } + NodeType::Atomic(operation) => { + return self.gen_atomic(node, operation, options); + } + NodeType::InlineAsm(_, _) => return self.gen_inline_assembly(node, options), + NodeType::ZeroFill { bytes } => { + let mut lines = vec![ self.gen_addr(node.lhs.as_ref().unwrap(), options), - Self::pop(X8), - Assembly::inst2(MOV, X13, 1), - if let Some(t) = node.lhs.as_ref().unwrap().dest_type() { - vec![ - Assembly::inst2(MOV, X10, t.size_of()), - Assembly::inst3(MUL, X13, X13, X10), - ] - .into() - } else { - vec![].into() - }, - Assembly::inst2(MOV, X11, X8), - self.deref(node.lhs.as_ref().unwrap()), - Assembly::inst3(op, X13, X8, X13), - Self::normalize_integer(&node.resolve_type().unwrap(), X13), - Assembly::inst2(STR, X13, Ptr(X11, node.resolve_type().unwrap().size_of())), - Self::push(X8), - ] - .into(); + Self::pop(X11), + Assembly::inst2(MOV, X9, 0), + ]; + if bytes >= 8 { + lines.extend([ + Assembly::inst2(MOV, X10, bytes / 8), + // Numeric labels stay local to this closed instruction sequence. + "901:".into(), + Assembly::inst2(STR, X9, Ptr(X11, 8)), + Assembly::inst3(ADD, X11, X11, 8), + Assembly::inst3(SUB, X10, X10, 1), + Assembly::inst2(CMP, X10, 0), + Assembly::inst1(JNE, "901b"), + ]); + } + for _ in 0..bytes % 8 { + lines.extend([ + Assembly::inst2(STR, X9, Ptr(X11, 1)), + Assembly::inst3(ADD, X11, X11, 1), + ]); + } + lines.push(Self::push(0)); + return lines.into(); + } + NodeType::CopyBytes { bytes } => { + let mut lines = vec![ + self.gen_addr(node.lhs.as_ref().unwrap(), options), + self.gen_addr(node.rhs.as_ref().unwrap(), options), + Self::pop(X10), + Self::pop(X11), + Self::push(X11), + ]; + if bytes >= 8 { + lines.extend([ + Assembly::inst2(MOV, X9, bytes / 8), + "902:".into(), + Assembly::inst2(LDR, X12, Ptr(X10, 8)), + Assembly::inst2(STR, X12, Ptr(X11, 8)), + Assembly::inst3(ADD, X10, X10, 8), + Assembly::inst3(ADD, X11, X11, 8), + Assembly::inst3(SUB, X9, X9, 1), + Assembly::inst2(CMP, X9, 0), + Assembly::inst1(JNE, "902b"), + ]); + } + for _ in 0..bytes % 8 { + lines.extend([ + Assembly::inst2(LDRU, X12, Ptr(X10, 1)), + Assembly::inst2(STR, X12, Ptr(X11, 1)), + Assembly::inst3(ADD, X10, X10, 1), + Assembly::inst3(ADD, X11, X11, 1), + ]); + } + return lines.into(); } _ => {} } @@ -1271,6 +1270,9 @@ impl<'a> AsmGenerator<'a> { Assembly::inst4(MSUB, X8, X10, X13, X8), ] .into(), + NodeType::BitAnd => Assembly::inst3(AND, X8, X8, X13), + NodeType::BitXor => Assembly::inst3(XOR, X8, X8, X13), + NodeType::BitOr => Assembly::inst3(OR, X8, X8, X13), NodeType::Eq | NodeType::Ne | NodeType::Lt | NodeType::Le => vec![ Assembly::inst2(CMP, X8, X13), Assembly::inst2( @@ -1298,9 +1300,6 @@ impl<'a> AsmGenerator<'a> { ), ] .into(), - NodeType::BitAnd => Assembly::inst3(AND, X8, X8, X13), - NodeType::BitXor => Assembly::inst3(XOR, X8, X8, X13), - NodeType::BitOr => Assembly::inst3(OR, X8, X8, X13), _ => { self.error_logger .print_error_position(node.token.as_ref().unwrap().pos, "unexpected node"); @@ -1320,19 +1319,19 @@ impl<'a> AsmGenerator<'a> { self.gen_node(node, options), if matches!( node.nt, - NodeType::Block - | NodeType::DefVar + NodeType::DefVar + | NodeType::Block + | NodeType::Return | NodeType::If | NodeType::While | NodeType::DoWhile | NodeType::For | NodeType::Switch | NodeType::Case - | NodeType::Label - | NodeType::Goto | NodeType::Break | NodeType::Continue - | NodeType::Return + | NodeType::Goto + | NodeType::Label ) { vec![].into() } else { @@ -1347,22 +1346,11 @@ impl<'a> AsmGenerator<'a> { fn gen_addr(&self, node: &Node, options: Options) -> Assembly { match node.nt { - NodeType::CopyBytes { .. } => self.gen_node(node, options), - NodeType::CallFunc => self.gen_node(node, options), - NodeType::Conditional => self.gen_node(node, options), - - NodeType::Sequence => { - let mut lines = vec![]; - for child in &node.children[..node.children.len() - 1] { - lines.push(self.gen_node(child, options)); - if !matches!(child.nt, NodeType::Block | NodeType::DefVar) { - lines.push(Self::pop(X8)); - } - } - lines.push(self.gen_addr(node.children.last().unwrap(), options)); - lines.into() - } - + NodeType::LocalVar => vec![ + Assembly::inst3(ADD, X8, X29, options.offset - node.offset.unwrap()), + Self::push(X8), + ] + .into(), NodeType::GlobalVar => vec![ if !node.dest.is_empty() { vec![ @@ -1370,8 +1358,8 @@ impl<'a> AsmGenerator<'a> { ADRP, X8, match self.target_os { - Os::MacOS => node.dest.clone().replace('@', "L_") + "@PAGE", Os::Linux => node.dest.clone().replace('@', "L_"), + Os::MacOS => node.dest.clone().replace('@', "L_") + "@PAGE", }, ), Assembly::inst3( @@ -1379,10 +1367,10 @@ impl<'a> AsmGenerator<'a> { X8, X8, match self.target_os { - Os::MacOS => node.dest.clone().replace('@', "L_") + "@PAGEOFF", Os::Linux => { ":lo12:".to_string() + &node.dest.clone().replace('@', "L_") } + Os::MacOS => node.dest.clone().replace('@', "L_") + "@PAGEOFF", }, ), ] @@ -1390,8 +1378,8 @@ impl<'a> AsmGenerator<'a> { } else if !self.defined_symbols.contains(&node.global_name) { let symbol = self.with_prefix(&node.global_name); let (page, offset) = match self.target_os { - Os::MacOS => (format!("{symbol}@GOTPAGE"), format!("{symbol}@GOTPAGEOFF")), Os::Linux => (format!(":got:{symbol}"), format!(":got_lo12:{symbol}")), + Os::MacOS => (format!("{symbol}@GOTPAGE"), format!("{symbol}@GOTPAGEOFF")), }; vec![ Assembly::inst2(ADRP, X8, page), @@ -1404,8 +1392,8 @@ impl<'a> AsmGenerator<'a> { ADRP, X8, match self.target_os { - Os::MacOS => self.with_prefix(&node.global_name) + "@PAGE", Os::Linux => self.with_prefix(&node.global_name), + Os::MacOS => self.with_prefix(&node.global_name) + "@PAGE", }, ), Assembly::inst3( @@ -1413,11 +1401,11 @@ impl<'a> AsmGenerator<'a> { X8, X8, match self.target_os { - Os::MacOS => self.with_prefix(&node.global_name) + "@PAGEOFF", Os::Linux => { ":lo12:".to_string() + &self.with_prefix(&node.global_name).replace('@', "L_") } + Os::MacOS => self.with_prefix(&node.global_name) + "@PAGEOFF", }, ), ] @@ -1426,12 +1414,21 @@ impl<'a> AsmGenerator<'a> { Self::push(X8), ] .into(), - NodeType::LocalVar => vec![ - Assembly::inst3(ADD, X8, X29, options.offset - node.offset.unwrap()), - Self::push(X8), - ] - .into(), NodeType::Deref => self.gen_node(node.lhs.as_ref().unwrap(), options), + NodeType::Conditional => self.gen_node(node, options), + NodeType::CallFunc => self.gen_node(node, options), + NodeType::Sequence => { + let mut lines = vec![]; + for child in &node.children[..node.children.len() - 1] { + lines.push(self.gen_node(child, options)); + if !matches!(child.nt, NodeType::DefVar | NodeType::Block) { + lines.push(Self::pop(X8)); + } + } + lines.push(self.gen_addr(node.children.last().unwrap(), options)); + lines.into() + } + NodeType::CopyBytes { .. } => self.gen_node(node, options), _ => { unreachable!(); } diff --git a/src/generator/aarch64/generator/atomic.rs b/src/generator/aarch64/generator/atomic.rs index e4b1f1f..a2f1c2c 100644 --- a/src/generator/aarch64/generator/atomic.rs +++ b/src/generator/aarch64/generator/atomic.rs @@ -17,12 +17,12 @@ impl AsmGenerator<'_> { Assembly::inst2(LDAXR, X10, Ptr(X8, ty.size_of())), ]; lines.push(match operation { - AtomicRmwOperation::Exchange => Assembly::inst2(MOV, X11, X9), AtomicRmwOperation::Nand => vec![ Assembly::inst3(AND, X11, X10, X9), Assembly::inst2(MVN, X11, X11), ] .into(), + AtomicRmwOperation::Exchange => Assembly::inst2(MOV, X11, X9), _ => Assembly::inst3( match operation { AtomicRmwOperation::Add => ADD, diff --git a/src/generator/aarch64/generator/globals.rs b/src/generator/aarch64/generator/globals.rs index 54d91bc..50d70bd 100644 --- a/src/generator/aarch64/generator/globals.rs +++ b/src/generator/aarch64/generator/globals.rs @@ -45,8 +45,8 @@ impl AsmGenerator<'_> { pub(super) fn weak_reference(&self, name: &str) -> Assembly { let directive = match self.target_os { - Os::MacOS => ".weak_reference", Os::Linux => ".weak", + Os::MacOS => ".weak_reference", }; format!("{directive} {}", self.with_prefix(name)).into() } @@ -62,8 +62,8 @@ impl AsmGenerator<'_> { if gv.common && !gv.internal && !gv.thread_local && gv.data.is_none() { let alignment = gv.ty.align_of().max(gv.alignment).max(1); let alignment = match self.target_os { - Os::MacOS => alignment.trailing_zeros() as usize, Os::Linux => alignment, + Os::MacOS => alignment.trailing_zeros() as usize, }; return format!( ".comm {},{},{}", @@ -106,7 +106,94 @@ impl AsmGenerator<'_> { data }; match ty { - Type::Aligned(child, _) => self.gen_initializer_element(child, data), + Type::Bool | Type::Integer { .. } | Type::LongLong { .. } => format!( + " {} {}", + if ty.size_of() == 1 { + ".byte".to_string() + } else { + format!(".{}byte", ty.size_of()) + }, + if let Some(GlobalVariableData::Address { symbol, offset }) = data { + format!("{}{offset:+}", self.address_symbol(symbol)) + } else if let Some(GlobalVariableData::Elem(value)) = data { + ty.convert_constant(value.parse().unwrap()).to_string() + } else { + "0".into() + } + ) + .into(), + Type::I8 | Type::SignedChar => format!( + " .byte {}", + if let Some(GlobalVariableData::Elem(s)) = data { + s + } else { + "0" + } + ) + .into(), + Type::I32 => format!( + " .4byte {}", + if let Some(GlobalVariableData::Elem(s)) = data { + s + } else { + "0" + } + ) + .into(), + Type::Integer128 { .. } => " .zero 16".into(), + Type::Enum(value) => self.gen_initializer_element(&value.underlying.borrow(), data), + Type::Float16 => " .zero 2".into(), + Type::LongDouble(bits) if *bits != 64 => { + let value = if let Some(GlobalVariableData::Elem(value)) = data { + value.parse::().unwrap() + } else { + 0 + }; + format!( + " .8byte {}\n .8byte {}", + value as u64, + (value >> 64) as u64 + ) + .into() + } + Type::Float(_) | Type::LongDouble(_) => format!( + " .{}byte {}", + ty.size_of(), + if let Some(GlobalVariableData::Elem(value)) = data { + value.as_str() + } else { + "0" + } + ) + .into(), + Type::Arr(children_ty, size) => { + let size = size.unwrap_or(0); + if let Some(GlobalVariableData::Arr(v)) = data { + vec![ + v.iter() + .enumerate() + .filter(|(i, _)| *i < size) + .map(|(_, d)| { + self.gen_initializer_element(children_ty.as_ref(), Some(d)) + }) + .collect::>() + .into(), + format!( + " .zero {}", + children_ty.size_of() + * v.iter() + .enumerate() + .filter(|(i, _)| *i < size) + .fold(size, |_, _| { size.saturating_sub(v.len()) }) + ) + .into(), + ] + .into() + } else { + format!(" .zero {}", children_ty.size_of() * size).into() + } + } + Type::Vector(..) => format!(" .zero {}", ty.size_of()).into(), Type::Struct(struct_type) if struct_type.has_bit_fields() || struct_type.definition().borrow().is_union => { @@ -159,94 +246,7 @@ impl AsmGenerator<'_> { } lines.into() } - Type::Arr(children_ty, size) => { - let size = size.unwrap_or(0); - if let Some(GlobalVariableData::Arr(v)) = data { - vec![ - v.iter() - .enumerate() - .filter(|(i, _)| *i < size) - .map(|(_, d)| { - self.gen_initializer_element(children_ty.as_ref(), Some(d)) - }) - .collect::>() - .into(), - format!( - " .zero {}", - children_ty.size_of() - * v.iter() - .enumerate() - .filter(|(i, _)| *i < size) - .fold(size, |_, _| { size.saturating_sub(v.len()) }) - ) - .into(), - ] - .into() - } else { - format!(" .zero {}", children_ty.size_of() * size).into() - } - } - Type::Enum(value) => self.gen_initializer_element(&value.underlying.borrow(), data), - Type::I8 | Type::SignedChar => format!( - " .byte {}", - if let Some(GlobalVariableData::Elem(s)) = data { - s - } else { - "0" - } - ) - .into(), - Type::Integer128 { .. } => " .zero 16".into(), - Type::Float16 => " .zero 2".into(), - Type::Vector(..) => format!(" .zero {}", ty.size_of()).into(), - Type::LongDouble(bits) if *bits != 64 => { - let value = if let Some(GlobalVariableData::Elem(value)) = data { - value.parse::().unwrap() - } else { - 0 - }; - format!( - " .8byte {}\n .8byte {}", - value as u64, - (value >> 64) as u64 - ) - .into() - } - Type::Float(_) | Type::LongDouble(_) => format!( - " .{}byte {}", - ty.size_of(), - if let Some(GlobalVariableData::Elem(value)) = data { - value.as_str() - } else { - "0" - } - ) - .into(), - Type::Integer { .. } | Type::LongLong { .. } | Type::Bool => format!( - " {} {}", - if ty.size_of() == 1 { - ".byte".to_string() - } else { - format!(".{}byte", ty.size_of()) - }, - if let Some(GlobalVariableData::Address { symbol, offset }) = data { - format!("{}{offset:+}", self.address_symbol(symbol)) - } else if let Some(GlobalVariableData::Elem(value)) = data { - ty.convert_constant(value.parse().unwrap()).to_string() - } else { - "0".into() - } - ) - .into(), - Type::I32 => format!( - " .4byte {}", - if let Some(GlobalVariableData::Elem(s)) = data { - s - } else { - "0" - } - ) - .into(), + Type::Aligned(child, _) => self.gen_initializer_element(child, data), _ => format!( " .8byte {}", if let Some(GlobalVariableData::Address { symbol, offset }) = data { diff --git a/src/generator/aarch64/instruction.rs b/src/generator/aarch64/instruction.rs index bcb62dd..bfc1c99 100644 --- a/src/generator/aarch64/instruction.rs +++ b/src/generator/aarch64/instruction.rs @@ -85,67 +85,67 @@ impl InstOperator { return "movzb"; } match self { - PRFM => "prfm", - InlineAsm => "", - FMOV => "fmov", - FADD => "fadd", - FSUB => "fsub", - FMUL => "fmul", - FDIV => "fdiv", - FCMP => "fcmp", - FCVT => "fcvt", - SCVTF => "scvtf", - UCVTF => "ucvtf", - FCVTZS => "fcvtzs", - FCVTZU => "fcvtzu", - CLZ => "clz", - RBIT => "rbit", MOV => "mov", - CSET => "cset", SXTB => "sxtb", SXTW => "sxtw", - MVN => "mvn", + LDR => "ldr", + STR => "str", + LDRU => "ldr", + STP => "stp", + LDP => "ldp", + ADRP => "adrp", + LEA => "lea", + MOVSX => "movsx", + MOVSXD => "movsxd", + MOVZX => "movzx", ADD => "add", SUB => "sub", - RET => "ret", - BL => "bl", - BLR => "blr", - CQO => "cqo", - SVC => "svc", - JMP => "b", - JE => "b.eq", - JNE => "b.ne", MUL => "mul", + MSUB => "msub", SDIV => "sdiv", UDIV => "udiv", - SHR => "lsr", - LDRU => "ldr", - MSUB => "msub", + CQO => "cqo", + AND => "and", + OR => "orr", + XOR => "eor", + MVN => "mvn", + NOT => "not", SHL => "lsl", + SHR => "lsr", SAR => "asr", - NOT => "not", + CLZ => "clz", + RBIT => "rbit", + CMP => "cmp", + CSET => "cset", SETE => "sete", SETNE => "setne", SETL => "setl", SETLE => "setle", - LEA => "lea", - MOVSX => "movsx", - MOVSXD => "movsxd", - MOVZX => "movzx", - CMP => "cmp", - AND => "and", - OR => "orr", - XOR => "eor", + JMP => "b", + JE => "b.eq", + JNE => "b.ne", + BL => "bl", + BLR => "blr", + RET => "ret", + SVC => "svc", + FMOV => "fmov", + FADD => "fadd", + FSUB => "fsub", + FMUL => "fmul", + FDIV => "fdiv", + FCMP => "fcmp", + FCVT => "fcvt", + SCVTF => "scvtf", + UCVTF => "ucvtf", + FCVTZS => "fcvtzs", + FCVTZU => "fcvtzu", CLREX => "clrex", LDAXR => "ldaxr", STLXR => "stlxr", LDAR => "ldar", STLR => "stlr", - LDR => "ldr", - STR => "str", - ADRP => "adrp", - STP => "stp", - LDP => "ldp", + PRFM => "prfm", + InlineAsm => "", NOP => "nop", } } @@ -197,8 +197,6 @@ impl Display for Register { return write!(f, "{}{index}", if *bits == 32 { "s" } else { "d" }); } f.write_str(match self { - Self::Float(_, _) => unreachable!(), - X0 => "x0", W0 => "w0", X1 => "x1", @@ -223,12 +221,13 @@ impl Display for Register { X20 => "x20", X29 => "x29", X30 => "x30", + SP => "sp", + Self::Float(_, _) => unreachable!(), AL => "al", CL => "cl", RIP => "rip", R8 => "r8", R9 => "r9", - SP => "sp", })?; Ok(()) } @@ -252,16 +251,16 @@ pub enum InstOperand { impl Display for InstOperand { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { f.write_str(&match self { - InstOperand::InlineAsmTemplate(template) => template.clone(), InstOperand::Reg(r) => format!("{}", r), InstOperand::Num(i) => format!("#{}", i), - InstOperand::Label(l) => l.clone(), InstOperand::Str(s) => String::from(*s), + InstOperand::Label(l) => l.clone(), InstOperand::Ptr(r, _) => format!("[{}]", r), - InstOperand::ElseFlag(i) => format!(".Lelse{}", i), + InstOperand::PtrAdd(s, r) => format!("[{}, {}]", s, r), InstOperand::BeginFlag(i) => format!(".Lbegin{}", i), + InstOperand::ElseFlag(i) => format!(".Lelse{}", i), InstOperand::EndFlag(i) => format!(".Lend{}", i), - InstOperand::PtrAdd(s, r) => format!("[{}, {}]", s, r), + InstOperand::InlineAsmTemplate(template) => template.clone(), }) } } @@ -339,8 +338,17 @@ impl Instruction { if let (Some(InstOperand::Reg(register)), Some(InstOperand::Ptr(base, size))) = (&self.operand1, &self.operand2) { - if matches!(self.operator, LDR | LDRU | STR | LDAR | STLR | LDAXR) { + if matches!(self.operator, LDR | STR | LDRU | LDAXR | LDAR | STLR) { let (operator, operand) = match (&self.operator, size) { + (LDR, 1) => ("ldrsb", register.to_string()), + (LDR, 2) => ("ldrsh", register.to_string()), + (LDR, 4) => ("ldrsw", register.to_string()), + (STR, 2) => ("strh", register.to_string().replacen('x', "w", 1)), + (STR, 1) => ("strb", register.to_string().replacen('x', "w", 1)), + (STR, 4) => ("str", register.to_string().replacen('x', "w", 1)), + (LDRU, 1) => ("ldrb", register.to_string().replacen('x', "w", 1)), + (LDRU, 2) => ("ldrh", register.to_string().replacen('x', "w", 1)), + (LDRU, 4) => ("ldr", register.to_string().replacen('x', "w", 1)), (LDAXR, 1) => ("ldaxrb", register.to_string().replacen('x', "w", 1)), (LDAXR, 2) => ("ldaxrh", register.to_string().replacen('x', "w", 1)), (LDAXR, 4) => ("ldaxr", register.to_string().replacen('x', "w", 1)), @@ -350,15 +358,6 @@ impl Instruction { (STLR, 1) => ("stlrb", register.to_string().replacen('x', "w", 1)), (STLR, 2) => ("stlrh", register.to_string().replacen('x', "w", 1)), (STLR, 4) => ("stlr", register.to_string().replacen('x', "w", 1)), - (LDR, 1) => ("ldrsb", register.to_string()), - (LDR, 2) => ("ldrsh", register.to_string()), - (LDRU, 1) => ("ldrb", register.to_string().replacen('x', "w", 1)), - (LDRU, 2) => ("ldrh", register.to_string().replacen('x', "w", 1)), - (LDRU, 4) => ("ldr", register.to_string().replacen('x', "w", 1)), - (STR, 2) => ("strh", register.to_string().replacen('x', "w", 1)), - (LDR, 4) => ("ldrsw", register.to_string()), - (STR, 1) => ("strb", register.to_string().replacen('x', "w", 1)), - (STR, 4) => ("str", register.to_string().replacen('x', "w", 1)), _ => (self.operator.to_string(target_os), register.to_string()), }; return format!(" {} {}, [{}]", operator, operand, base); diff --git a/src/generator/abi.rs b/src/generator/abi.rs index 405b358..54a6271 100644 --- a/src/generator/abi.rs +++ b/src/generator/abi.rs @@ -82,6 +82,11 @@ fn integer_slots(ty: &Type, offset: usize, slots: &mut [Option]) { } } } + Type::Arr(child, count) => { + for index in 0..count.unwrap_or(0) { + integer_slots(child, offset + index * child.size_of(), slots); + } + } Type::Struct(st) if st.definition().borrow().members.is_empty() && st.has_bit_fields() => { slots.fill(Some(true)) } @@ -101,11 +106,6 @@ fn integer_slots(ty: &Type, offset: usize, slots: &mut [Option]) { } } } - Type::Arr(child, count) => { - for index in 0..count.unwrap_or(0) { - integer_slots(child, offset + index * child.size_of(), slots); - } - } _ => { for slot in slots .iter_mut() @@ -119,13 +119,13 @@ fn integer_slots(ty: &Type, offset: usize, slots: &mut [Option]) { } fn has_unaligned_member(ty: &Type, offset: usize) -> bool { match ty.unqualified() { + Type::Arr(child, count) => (0..count.unwrap_or(0)) + .any(|index| has_unaligned_member(child, offset + index * child.size_of())), Type::Struct(record) => record.definition().borrow().members.iter().any(|member| { let offset = offset + member.offset; member.bit_field.is_none() && (offset % member.ty.align_of() != 0 || has_unaligned_member(&member.ty, offset)) }), - Type::Arr(child, count) => (0..count.unwrap_or(0)) - .any(|index| has_unaligned_member(child, offset + index * child.size_of())), _ => false, } } @@ -358,9 +358,9 @@ pub fn signature( arguments, result: result_parts, indirect_result, - stack_size: stack, - floating, general, + floating, + stack_size: stack, } } diff --git a/src/generator/llvm/generator.rs b/src/generator/llvm/generator.rs index 2218c59..85671b0 100644 --- a/src/generator/llvm/generator.rs +++ b/src/generator/llvm/generator.rs @@ -230,17 +230,27 @@ impl<'a> IrGenerator<'a> { fn gen_type(ty: &Type) -> String { match ty { - Type::Qualified(child, _) | Type::Aligned(child, _) => Self::gen_type(child), Type::Void => "void".to_string(), + Type::Bool => "i8".to_string(), + Type::I8 | Type::SignedChar => "i8".to_string(), + Type::I32 => "i32".to_string(), + Type::Integer { bits, .. } => format!("i{bits}"), + Type::LongLong { .. } => "i64".to_string(), // LLVM 15 gives i128 only 8-byte alignment on some targets. Type::Integer128 { .. } => "<2 x i64>".to_string(), + Type::Enum(value) => Self::gen_type(&value.underlying.borrow()), Type::Float16 => "half".to_string(), - Type::Vector(element, lanes) => format!("<{lanes} x {}>", Self::gen_type(element)), Type::LongDouble(80) => "x86_fp80".to_string(), Type::LongDouble(128) => "fp128".to_string(), Type::Float(bits) | Type::LongDouble(bits) => { if *bits == 32 { "float" } else { "double" }.to_string() } + Type::Ptr(_) => "ptr".to_string(), + Type::Arr(child, size) => { + format!("[{} x {}]", size.unwrap_or(0), Self::gen_type(child)) + } + Type::Vla(child, _) => format!("[0 x {}]", Self::gen_type(child)), + Type::Vector(element, lanes) => format!("<{lanes} x {}>", Self::gen_type(element)), Type::Struct(record) if record.definition().borrow().is_union || record.has_bit_fields() @@ -278,33 +288,23 @@ impl<'a> IrGenerator<'a> { } format!("{{ {} }}", fields.join(", ")) } - Type::Enum(value) => Self::gen_type(&value.underlying.borrow()), - Type::I8 | Type::SignedChar => "i8".to_string(), - Type::LongLong { .. } => "i64".to_string(), - Type::I32 => "i32".to_string(), - Type::Integer { bits, .. } => format!("i{bits}"), - Type::Bool => "i8".to_string(), - Type::Ptr(_) => "ptr".to_string(), - Type::Arr(child, size) => { - format!("[{} x {}]", size.unwrap_or(0), Self::gen_type(child)) - } - Type::Vla(child, _) => format!("[0 x {}]", Self::gen_type(child)), - Type::VariadicFunc(args, result) => format!( - "{} ({}, ...)", + Type::Func(args, result) => format!( + "{} ({})", Self::gen_type(result), args.iter() .map(Self::gen_type) .collect::>() .join(", ") ), - Type::Func(args, result) => format!( - "{} ({})", + Type::VariadicFunc(args, result) => format!( + "{} ({}, ...)", Self::gen_type(result), args.iter() .map(Self::gen_type) .collect::>() .join(", ") ), + Type::Qualified(child, _) | Type::Aligned(child, _) => Self::gen_type(child), } } @@ -357,16 +357,16 @@ impl<'a> IrGenerator<'a> { } let return_ty = result; let options = &mut Options { + return_ty: return_ty.clone(), + return_pointer: None, register_number: &mut func.args.len(), register_map: &mut HashMap::::new(), register_queue: &mut vec![], - label_number: 0, - terminated: false, - return_ty: return_ty.clone(), - return_pointer: None, - break_labels: vec![], expression_slots: HashMap::new(), argument_slots: HashMap::new(), + label_number: 0, + break_labels: vec![], + terminated: false, }; [ vec![format!( @@ -684,15 +684,22 @@ impl<'a> IrGenerator<'a> { fn gen_addr(&self, node: &Node, options: &mut Options) -> Vec { match node.nt { - NodeType::CopyBytes { .. } => self.gen_node(node, options), - NodeType::CallFunc => { - let lines = self.gen_node(node, options); - options.register_queue.pop(); - options - .register_queue - .push(format!("%{}", options.register_map[&node.offset.unwrap()])); - lines + NodeType::LocalVar => { + let register = options.register_from_offset(node.offset.unwrap()); + options.register_queue.push(format!("%{}", register)); + vec![] + } + NodeType::GlobalVar => { + let symbol = if node.dest.is_empty() { + self.symbol_name(&node.global_name) + } else { + node.dest.clone() + }; + // LLVM resolves TLS globals in the current thread, including on LLVM 15. + options.register_queue.push(symbol); + vec![] } + NodeType::Deref => self.gen_node(node.lhs.as_ref().unwrap(), options), NodeType::Conditional => { let lines = self.gen_node(node, options); options.register_queue.pop(); @@ -700,7 +707,14 @@ impl<'a> IrGenerator<'a> { options.register_queue.push(format!("%{}", slot)); lines } - + NodeType::CallFunc => { + let lines = self.gen_node(node, options); + options.register_queue.pop(); + options + .register_queue + .push(format!("%{}", options.register_map[&node.offset.unwrap()])); + lines + } NodeType::Sequence => { let mut lines = vec![]; let depth = options.register_queue.len(); @@ -711,23 +725,7 @@ impl<'a> IrGenerator<'a> { lines.extend(self.gen_addr(node.children.last().unwrap(), options)); lines } - - NodeType::LocalVar => { - let register = options.register_from_offset(node.offset.unwrap()); - options.register_queue.push(format!("%{}", register)); - vec![] - } - NodeType::GlobalVar => { - let symbol = if node.dest.is_empty() { - self.symbol_name(&node.global_name) - } else { - node.dest.clone() - }; - // LLVM resolves TLS globals in the current thread, including on LLVM 15. - options.register_queue.push(symbol); - vec![] - } - NodeType::Deref => self.gen_node(node.lhs.as_ref().unwrap(), options), + NodeType::CopyBytes { .. } => self.gen_node(node, options), _ => { self.error_logger.print_error_position( node.token.as_ref().unwrap().pos, @@ -770,80 +768,43 @@ impl<'a> IrGenerator<'a> { return lines; } match node.nt { - NodeType::Prefetch(write, locality) => { - return self.gen_prefetch(node, write, locality, options); - } - NodeType::InlineAsm(volatile, memory) => { - return self.gen_inline_assembly(node, volatile, memory, options); - } - NodeType::Atomic(operation) => { - return self.gen_atomic(node, operation, options); - } - NodeType::VaStart | NodeType::VaEnd => { - let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); - let pointer = options.register_queue.pop().unwrap(); - let intrinsic = if node.nt == NodeType::VaStart { - "va_start" + NodeType::Num => { + let ty = node.resolve_type_view().unwrap(); + options.register_queue.push(if ty.float_bits().is_some() { + node.extended_float.as_ref().map_or_else( + || Self::float_literal(&ty, node.value.unwrap()), + |bits| crate::floating::llvm_literal(**bits), + ) } else { - "va_end" - }; - lines.push(format!(" call void @llvm.{intrinsic}(ptr {pointer})")); - options.register_queue.push("0".into()); - return lines; - } - - NodeType::CopyBytes { bytes } => { - let mut lines = self.gen_addr(node.lhs.as_ref().unwrap(), options); - let destination = options.register_queue.pop().unwrap(); - lines.extend(self.gen_addr(node.rhs.as_ref().unwrap(), options)); - let source = options.register_queue.pop().unwrap(); - lines.push(format!( - " call void @llvm.memcpy.p0.p0.i64(ptr align 1 {destination}, ptr align 1 {source}, i64 {bytes}, i1 false)" - )); - options.register_queue.push(destination); - return lines; - } - NodeType::ZeroFill { bytes } => { - let mut lines = self.gen_addr(node.lhs.as_ref().unwrap(), options); - let pointer = options.register_queue.pop().unwrap(); - lines.push(format!( - " call void @llvm.memset.p0.i64(ptr align 1 {pointer}, i8 0, i64 {bytes}, i1 false)" - )); - options.register_queue.push("0".into()); - return lines; + node.value.unwrap().to_string() + }); + return vec![]; } - NodeType::BitScan { bits, leading } => { - let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); - let operand = options.register_queue.pop().unwrap(); - let count = options.new_temporary(); - let intrinsic = if leading { "ctlz" } else { "cttz" }; - lines.push(format!( - " %{count} = call i{bits} @llvm.{intrinsic}.i{bits}(i{bits} {operand}, i1 false)" - )); - if bits == 64 { - let result = options.new_register(); - lines.push(format!(" %{result} = trunc i64 %{count} to i32")); - } else { - options.register_queue.push(format!("%{count}")); + NodeType::LocalVar | NodeType::GlobalVar | NodeType::Deref => { + let mut lines = self.gen_addr(node, options); + let ty = node.resolve_type_view().unwrap(); + if !matches!( + *ty, + Type::Arr(..) | Type::Vla(..) | Type::Func(..) | Type::VariadicFunc(..) + ) { + let address = options.register_queue.pop().unwrap(); + lines.push(format!( + " %{} = load {}{}, ptr {}, align {}", + options.new_register(), + if node.volatile_access { + "volatile " + } else { + "" + }, + Self::gen_type_with_opaque_ptr(&ty), + address, + Self::access_alignment(node) + )); } return lines; } - NodeType::FloatAbs => { - let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); - let value = options.register_queue.pop().unwrap(); - let ty = node.resolve_type_view().unwrap(); - let bits = ty.float_bits().unwrap(); - let float = Self::gen_type(&ty); - let raw = options.new_temporary(); - let masked = options.new_temporary(); - let result = options.new_register(); - let mask = (1u128 << (bits - 1)) - 1; - lines.extend([ - format!(" %{raw} = bitcast {float} {value} to i{bits}"), - format!(" %{masked} = and i{bits} %{raw}, {mask}"), - format!(" %{result} = bitcast i{bits} %{masked} to {float}"), - ]); - return lines; + NodeType::Addr => { + return self.gen_addr(node.lhs.as_ref().unwrap(), options); } NodeType::Cast => { let operand = node.lhs.as_ref().unwrap(); @@ -864,39 +825,314 @@ impl<'a> IrGenerator<'a> { } return lines; } - NodeType::Sequence => { - let mut lines = vec![]; - let depth = options.register_queue.len(); - for (index, child) in node.children.iter().enumerate() { - lines.extend(self.gen_node(child, options)); - if index + 1 < node.children.len() { - options.register_queue.truncate(depth); - } - } + NodeType::Sub + if node.lhs.as_ref().unwrap().dest_type_view().is_some() + && node.rhs.as_ref().unwrap().dest_type_view().is_some() => + { + let mut lines = self.gen_node(node.rhs.as_ref().unwrap(), options); + let rhs = options.register_queue.pop().unwrap(); + lines.extend(self.gen_node(node.lhs.as_ref().unwrap(), options)); + let lhs = options.register_queue.pop().unwrap(); + let left = options.new_temporary(); + lines.push(format!(" %{} = ptrtoint ptr {} to i64", left, lhs)); + let right = options.new_temporary(); + lines.push(format!(" %{} = ptrtoint ptr {} to i64", right, rhs)); + let difference = options.new_temporary(); + lines.push(format!(" %{} = sub i64 %{}, %{}", difference, left, right)); + let count = options.new_temporary(); + lines.push(format!( + " %{} = sdiv i64 %{}, {}", + count, + difference, + node.lhs + .as_ref() + .unwrap() + .dest_type_view() + .unwrap() + .size_of() + )); + lines.push(format!( + " %{} = trunc i64 %{} to i32", + options.new_register(), + count + )); return lines; } - NodeType::DefVar => { - return node - .children - .iter() - .flat_map(|node| self.gen_node(node, options)) - .collect(); - } - NodeType::CallFunc => { - if matches!(node.resolve_type_view().as_deref(), Some(Type::Struct(_))) - || node.args.iter().any(|arg| { - matches!(arg.resolve_type_view().as_deref(), Some(Type::Struct(_))) - }) - { - return self.aggregate_call(node, options); - } - let parameters = &node.parameter_types; - let result = node.cty.as_ref().unwrap(); - let variadic = node.variadic; - let mut lines = vec![]; - let callee = if let Some(callee) = &node.lhs { - lines.extend(self.gen_node(callee, options)); - options.register_queue.pop().unwrap() + NodeType::Add | NodeType::Sub + if node.lhs.as_ref().unwrap().dest_type_view().is_some() => + { + let base = node.lhs.as_ref().unwrap(); + let index = node.rhs.as_ref().unwrap(); + let mut lines = self.gen_node(index, options); + let mut value = options.register_queue.pop().unwrap(); + // GEP sign-extends its indices, so preserve C signedness explicitly. + value = Self::convert_integer( + value, + &index.resolve_type_view().unwrap(), + &Type::integer(64, true), + options, + &mut lines, + ); + let index_ty = "i64"; + if matches!(node.nt, NodeType::Sub) { + let register = options.new_temporary(); + lines.push(format!(" %{} = sub {} 0, {}", register, index_ty, value)); + value = format!("%{}", register); + } + lines.extend(self.gen_node(base, options)); + let address = options.register_queue.pop().unwrap(); + lines.push(format!( + " %{} = getelementptr inbounds {}, ptr {}, {} {}", + options.new_register(), + Self::gen_type_with_opaque_ptr(&base.dest_type_view().unwrap()), + address, + index_ty, + value + )); + return lines; + } + NodeType::BitNot => { + let operand = node.lhs.as_ref().unwrap(); + let mut lines = self.gen_node(operand, options); + let value = options.register_queue.pop().unwrap(); + let value = Self::convert_integer( + value, + &operand.resolve_type_view().unwrap(), + &node.resolve_type_view().unwrap(), + options, + &mut lines, + ); + lines.push(format!( + " %{} = xor {} {}, -1", + options.new_register(), + Self::gen_type(&node.resolve_type_view().unwrap()), + value + )); + return lines; + } + NodeType::LogicalAnd | NodeType::LogicalOr => { + let mut lines = vec![]; + let predicate = self.gen_predicate(node.lhs.as_ref().unwrap(), options, &mut lines); + let slot = options.expression_slots[&node.token.as_ref().unwrap().pos]; + let rhs_label = options.new_label(); + let short_label = options.new_label(); + let end_label = options.new_label(); + let is_and = matches!(node.nt, NodeType::LogicalAnd); + lines.push(format!( + " br i1 {}, label %{}, label %{}", + predicate, + if is_and { &rhs_label } else { &short_label }, + if is_and { &short_label } else { &rhs_label } + )); + lines.push(format!("{}:", short_label)); + lines.push(format!( + " store i32 {}, ptr %{}, align 4", + if is_and { 0 } else { 1 }, + slot + )); + lines.push(format!(" br label %{}", end_label)); + lines.push(format!("{}:", rhs_label)); + let predicate = self.gen_predicate(node.rhs.as_ref().unwrap(), options, &mut lines); + let value = options.new_temporary(); + lines.push(format!(" %{} = zext i1 {} to i32", value, predicate)); + lines.push(format!(" store i32 %{}, ptr %{}, align 4", value, slot)); + lines.push(format!(" br label %{}", end_label)); + lines.push(format!("{}:", end_label)); + lines.push(format!( + " %{} = load i32, ptr %{}, align 4", + options.new_register(), + slot + )); + return lines; + } + NodeType::Conditional => { + if node.resolve_type_view().as_deref() == Some(&Type::Void) { + let mut lines = vec![]; + let predicate = + self.gen_predicate(node.cond.as_ref().unwrap(), options, &mut lines); + let then_label = options.new_label(); + let else_label = options.new_label(); + let end_label = options.new_label(); + lines.push(format!( + " br i1 {}, label %{}, label %{}", + predicate, then_label, else_label + )); + for (label, branch) in [ + (&then_label, node.then.as_ref().unwrap()), + (&else_label, node.els.as_ref().unwrap()), + ] { + lines.push(format!("{}:", label)); + lines.extend(self.gen_node(branch, options)); + options.register_queue.pop(); + lines.push(format!(" br label %{}", end_label)); + } + lines.push(format!("{}:", end_label)); + options.register_queue.push("0".to_string()); + return lines; + } + + let mut lines = vec![]; + let ty = node.resolve_type_view().unwrap(); + let slot = options.expression_slots[&node.token.as_ref().unwrap().pos]; + let predicate = + self.gen_predicate(node.cond.as_ref().unwrap(), options, &mut lines); + let then_label = options.new_label(); + let else_label = options.new_label(); + let end_label = options.new_label(); + lines.push(format!( + " br i1 {}, label %{}, label %{}", + predicate, then_label, else_label + )); + for (label, branch) in [ + (&then_label, node.then.as_ref().unwrap()), + (&else_label, node.els.as_ref().unwrap()), + ] { + lines.push(format!("{}:", label)); + lines.extend(self.gen_node(branch, options)); + let value = options.register_queue.pop().unwrap(); + let value = Self::convert_integer( + value, + &branch.resolve_type_view().unwrap(), + &ty, + options, + &mut lines, + ); + lines.push(format!( + " store {} {}, ptr %{}, align {}", + Self::gen_type(&ty), + value, + slot, + Self::align(&ty) + )); + lines.push(format!(" br label %{}", end_label)); + } + lines.push(format!("{}:", end_label)); + lines.push(format!( + " %{} = load {}, ptr %{}, align {}", + options.new_register(), + Self::gen_type(&ty), + slot, + Self::align(&ty) + )); + return lines; + } + NodeType::Assign => { + let mut lines = self.gen_node(node.rhs.as_ref().unwrap(), options); + let value = options.register_queue.pop().unwrap(); + lines.extend(self.gen_addr(node.lhs.as_ref().unwrap(), options)); + let address = options.register_queue.pop().unwrap(); + let ty = node.lhs.as_ref().unwrap().resolve_type_view().unwrap(); + let source_ty = node.rhs.as_ref().unwrap().resolve_type_view().unwrap(); + let value = Self::convert_integer(value, &source_ty, &ty, options, &mut lines); + lines.push(format!( + " store {}{} {}, ptr {}, align {}", + if node.lhs.as_ref().is_some_and(|lhs| lhs.volatile_access) { + "volatile " + } else { + "" + }, + Self::gen_type_with_opaque_ptr(&ty), + value, + address, + Self::access_alignment(node.lhs.as_ref().unwrap()) + )); + options.register_queue.push(value); + return lines; + } + NodeType::SuffixIncr | NodeType::SuffixDecr => { + let operand = node.lhs.as_ref().unwrap(); + let mut lines = self.gen_addr(operand, options); + let address = options.register_queue.pop().unwrap(); + let ty = operand.resolve_type_view().unwrap(); + let old = options.new_temporary(); + lines.push(format!( + " %{} = load {}{}, ptr {}, align {}", + old, + if node.volatile_access { + "volatile " + } else { + "" + }, + Self::gen_type(&ty), + address, + Self::access_alignment(operand) + )); + let new = options.new_temporary(); + if let Type::Ptr(child) = ty.as_ref() { + lines.push(format!( + " %{} = getelementptr inbounds {}, ptr %{}, i32 {}", + new, + Self::gen_type(child), + old, + if matches!(node.nt, NodeType::SuffixIncr) { + 1 + } else { + -1 + } + )); + } else { + lines.push(format!( + " %{} = {} {} %{}, {}", + new, + if matches!(node.nt, NodeType::SuffixIncr) { + if ty.float_bits().is_some() { + "fadd" + } else { + "add" + } + } else if ty.float_bits().is_some() { + "fsub" + } else { + "sub" + }, + Self::gen_type(&ty), + old, + if ty.float_bits().is_some() { + Self::float_literal(&ty, ty.float_constant(1.0)) + } else { + "1".to_string() + } + )); + } + let new = if *ty == Type::Bool { + let predicate = options.new_temporary(); + lines.push(format!(" %{predicate} = icmp ne i8 %{new}, 0")); + let result = options.new_temporary(); + lines.push(format!(" %{result} = zext i1 %{predicate} to i8")); + result + } else { + new + }; + lines.push(format!( + " store {}{} %{}, ptr {}, align {}", + if node.volatile_access { + "volatile " + } else { + "" + }, + Self::gen_type(&ty), + new, + address, + Self::access_alignment(operand) + )); + options.register_queue.push(format!("%{}", old)); + return lines; + } + NodeType::CallFunc => { + if matches!(node.resolve_type_view().as_deref(), Some(Type::Struct(_))) + || node.args.iter().any(|arg| { + matches!(arg.resolve_type_view().as_deref(), Some(Type::Struct(_))) + }) + { + return self.aggregate_call(node, options); + } + let parameters = &node.parameter_types; + let result = node.cty.as_ref().unwrap(); + let variadic = node.variadic; + let mut lines = vec![]; + let callee = if let Some(callee) = &node.lhs { + lines.extend(self.gen_node(callee, options)); + options.register_queue.pop().unwrap() } else { self.symbol_name(&node.global_name) }; @@ -951,6 +1187,24 @@ impl<'a> IrGenerator<'a> { )); return lines; } + NodeType::Sequence => { + let mut lines = vec![]; + let depth = options.register_queue.len(); + for (index, child) in node.children.iter().enumerate() { + lines.extend(self.gen_node(child, options)); + if index + 1 < node.children.len() { + options.register_queue.truncate(depth); + } + } + return lines; + } + NodeType::DefVar => { + return node + .children + .iter() + .flat_map(|node| self.gen_node(node, options)) + .collect(); + } NodeType::Block => { return node .children @@ -961,292 +1215,72 @@ impl<'a> IrGenerator<'a> { NodeType::Return => { if matches!(options.return_ty, Type::Struct(_)) { return self.aggregate_return(node, options); - } - if options.return_ty == Type::Void { - let mut lines = if let Some(operand) = &node.lhs { - self.gen_node(operand, options) - } else { - vec![] - }; - if node.lhs.is_some() { - options.register_queue.pop(); - } - lines.push(" ret void".to_string()); - options.terminated = true; - return lines; - } - if node.lhs.is_none() { - options.terminated = true; - return vec![" ret void".to_string()]; - } - let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); - let value = options.register_queue.pop().unwrap(); - let source_ty = node.resolve_type_view().unwrap(); - let target_ty = options.return_ty.clone(); - let value = - Self::convert_integer(value, &source_ty, &target_ty, options, &mut lines); - lines.push(format!( - " ret {} {}", - Self::gen_type_with_opaque_ptr(&target_ty), - value - )); - options.terminated = true; - return lines; - } - NodeType::Num => { - let ty = node.resolve_type_view().unwrap(); - options.register_queue.push(if ty.float_bits().is_some() { - node.extended_float.as_ref().map_or_else( - || Self::float_literal(&ty, node.value.unwrap()), - |bits| crate::floating::llvm_literal(**bits), - ) - } else { - node.value.unwrap().to_string() - }); - return vec![]; - } - NodeType::StackAlloc => { - let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); - let size = options.register_queue.pop().unwrap(); - let register = options.new_register(); - lines.push(format!(" %{register} = alloca i8, i64 {size}, align 16")); - return lines; - } - NodeType::Assign => { - let mut lines = self.gen_node(node.rhs.as_ref().unwrap(), options); - let value = options.register_queue.pop().unwrap(); - lines.extend(self.gen_addr(node.lhs.as_ref().unwrap(), options)); - let address = options.register_queue.pop().unwrap(); - let ty = node.lhs.as_ref().unwrap().resolve_type_view().unwrap(); - let source_ty = node.rhs.as_ref().unwrap().resolve_type_view().unwrap(); - let value = Self::convert_integer(value, &source_ty, &ty, options, &mut lines); - lines.push(format!( - " store {}{} {}, ptr {}, align {}", - if node.lhs.as_ref().is_some_and(|lhs| lhs.volatile_access) { - "volatile " - } else { - "" - }, - Self::gen_type_with_opaque_ptr(&ty), - value, - address, - Self::access_alignment(node.lhs.as_ref().unwrap()) - )); - options.register_queue.push(value); - return lines; - } - NodeType::Addr => { - return self.gen_addr(node.lhs.as_ref().unwrap(), options); - } - NodeType::Deref | NodeType::LocalVar | NodeType::GlobalVar => { - let mut lines = self.gen_addr(node, options); - let ty = node.resolve_type_view().unwrap(); - if !matches!( - *ty, - Type::Arr(..) | Type::Vla(..) | Type::Func(..) | Type::VariadicFunc(..) - ) { - let address = options.register_queue.pop().unwrap(); - lines.push(format!( - " %{} = load {}{}, ptr {}, align {}", - options.new_register(), - if node.volatile_access { - "volatile " - } else { - "" - }, - Self::gen_type_with_opaque_ptr(&ty), - address, - Self::access_alignment(node) - )); - } - return lines; - } - NodeType::Sub - if node.lhs.as_ref().unwrap().dest_type_view().is_some() - && node.rhs.as_ref().unwrap().dest_type_view().is_some() => - { - let mut lines = self.gen_node(node.rhs.as_ref().unwrap(), options); - let rhs = options.register_queue.pop().unwrap(); - lines.extend(self.gen_node(node.lhs.as_ref().unwrap(), options)); - let lhs = options.register_queue.pop().unwrap(); - let left = options.new_temporary(); - lines.push(format!(" %{} = ptrtoint ptr {} to i64", left, lhs)); - let right = options.new_temporary(); - lines.push(format!(" %{} = ptrtoint ptr {} to i64", right, rhs)); - let difference = options.new_temporary(); - lines.push(format!(" %{} = sub i64 %{}, %{}", difference, left, right)); - let count = options.new_temporary(); - lines.push(format!( - " %{} = sdiv i64 %{}, {}", - count, - difference, - node.lhs - .as_ref() - .unwrap() - .dest_type_view() - .unwrap() - .size_of() - )); - lines.push(format!( - " %{} = trunc i64 %{} to i32", - options.new_register(), - count - )); - return lines; - } - NodeType::Add | NodeType::Sub - if node.lhs.as_ref().unwrap().dest_type_view().is_some() => - { - let base = node.lhs.as_ref().unwrap(); - let index = node.rhs.as_ref().unwrap(); - let mut lines = self.gen_node(index, options); - let mut value = options.register_queue.pop().unwrap(); - // GEP sign-extends its indices, so preserve C signedness explicitly. - value = Self::convert_integer( - value, - &index.resolve_type_view().unwrap(), - &Type::integer(64, true), - options, - &mut lines, - ); - let index_ty = "i64"; - if matches!(node.nt, NodeType::Sub) { - let register = options.new_temporary(); - lines.push(format!(" %{} = sub {} 0, {}", register, index_ty, value)); - value = format!("%{}", register); - } - lines.extend(self.gen_node(base, options)); - let address = options.register_queue.pop().unwrap(); - lines.push(format!( - " %{} = getelementptr inbounds {}, ptr {}, {} {}", - options.new_register(), - Self::gen_type_with_opaque_ptr(&base.dest_type_view().unwrap()), - address, - index_ty, - value - )); - return lines; - } - NodeType::BitNot => { - let operand = node.lhs.as_ref().unwrap(); - let mut lines = self.gen_node(operand, options); - let value = options.register_queue.pop().unwrap(); - let value = Self::convert_integer( - value, - &operand.resolve_type_view().unwrap(), - &node.resolve_type_view().unwrap(), - options, - &mut lines, - ); - lines.push(format!( - " %{} = xor {} {}, -1", - options.new_register(), - Self::gen_type(&node.resolve_type_view().unwrap()), - value - )); - return lines; - } - NodeType::Switch => { - let mut lines = self.gen_node(node.cond.as_ref().unwrap(), options); - let value = options.register_queue.pop().unwrap(); - let selector_ty = node - .cond - .as_ref() - .unwrap() - .resolve_type_view() - .unwrap() - .promoted(); - let value = Self::convert_integer( - value, - &node.cond.as_ref().unwrap().resolve_type_view().unwrap(), - &selector_ty, - options, - &mut lines, - ); - let end = options.new_label(); - let default = node - .children - .iter() - .find(|case| case.value.is_none()) - .map(|case| format!("case.{}", case.token.as_ref().unwrap().pos)) - .unwrap_or(end.clone()); - lines.push(format!( - " switch {} {}, label %{} [", - Self::gen_type(&selector_ty), - value, - default - )); - for case in &node.children { - if let Some(value) = case.value { - lines.push(format!( - " {} {}, label %case.{}", - Self::gen_type(&selector_ty), - value as i64, - case.token.as_ref().unwrap().pos - )); + } + if options.return_ty == Type::Void { + let mut lines = if let Some(operand) = &node.lhs { + self.gen_node(operand, options) + } else { + vec![] + }; + if node.lhs.is_some() { + options.register_queue.pop(); } + lines.push(" ret void".to_string()); + options.terminated = true; + return lines; } - lines.push(" ]".to_string()); - options.terminated = true; - options.break_labels.push(end.clone()); - lines.extend(self.gen_node(node.then.as_ref().unwrap(), options)); - options.break_labels.pop(); - if !options.terminated { - lines.push(format!(" br label %{}", end)); + if node.lhs.is_none() { + options.terminated = true; + return vec![" ret void".to_string()]; } - lines.push(format!("{}:", end)); - options.terminated = false; - return lines; - } - NodeType::Goto => { + let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); + let value = options.register_queue.pop().unwrap(); + let source_ty = node.resolve_type_view().unwrap(); + let target_ty = options.return_ty.clone(); + let value = + Self::convert_integer(value, &source_ty, &target_ty, options, &mut lines); + lines.push(format!( + " ret {} {}", + Self::gen_type_with_opaque_ptr(&target_ty), + value + )); options.terminated = true; - return vec![format!(" br label %goto.{}", node.value.unwrap())]; - } - NodeType::Label => { - let label = format!("goto.{}", node.value.unwrap()); - let mut lines = vec![]; - if !options.terminated { - lines.push(format!(" br label %{label}")); - } - lines.push(format!("{label}:")); - options.terminated = false; - lines.extend(self.gen_node(node.lhs.as_ref().unwrap(), options)); return lines; } - NodeType::Case => { - let label = format!("case.{}", node.token.as_ref().unwrap().pos); + NodeType::If => { let mut lines = vec![]; - if !options.terminated { - lines.push(format!(" br label %{}", label)); - } - lines.push(format!("{}:", label)); - options.terminated = false; - lines.extend(self.gen_node(node.lhs.as_ref().unwrap(), options)); - return lines; - } - NodeType::DoWhile => { - let body_label = options.new_label(); - let condition_label = format!("continue.{}", node.token.as_ref().unwrap().pos); - let end_label = options.new_label(); - let mut lines = vec![ - format!(" br label %{}", body_label), - format!("{}:", body_label), - ]; - options.break_labels.push(end_label.clone()); - lines.extend(self.gen_node(node.then.as_ref().unwrap(), options)); - options.break_labels.pop(); - if !options.terminated { - lines.push(format!(" br label %{}", condition_label)); - } - options.terminated = false; - lines.push(format!("{}:", condition_label)); let predicate = self.gen_predicate(node.cond.as_ref().unwrap(), options, &mut lines); + let then_label = options.new_label(); + let else_label = options.new_label(); + let end_label = options.new_label(); lines.push(format!( " br i1 {}, label %{}, label %{}", - predicate, body_label, end_label + predicate, then_label, else_label, )); - lines.push(format!("{}:", end_label)); + + lines.push(format!("{}:", then_label)); + options.terminated = false; + lines.extend(self.gen_node(node.then.as_ref().unwrap(), options)); + let then_terminated = options.terminated; + if !then_terminated { + lines.push(format!(" br label %{}", end_label)); + } + + lines.push(format!("{}:", else_label)); + options.terminated = false; + if let Some(els) = &node.els { + lines.extend(self.gen_node(els, options)); + } + let else_terminated = options.terminated; + if !else_terminated { + lines.push(format!(" br label %{}", end_label)); + } + + options.terminated = then_terminated && else_terminated; + if !options.terminated { + lines.push(format!("{}:", end_label)); + } return lines; } NodeType::While | NodeType::For => { @@ -1285,240 +1319,203 @@ impl<'a> IrGenerator<'a> { lines.push(format!("{}:", end_label)); return lines; } - NodeType::Continue => { - options.terminated = true; - return vec![format!(" br label %continue.{}", node.value.unwrap())]; - } - NodeType::Break => { - let Some(label) = options.break_labels.last() else { - self.error_logger.print_error_position( - node.token.as_ref().unwrap().pos, - "unexpected break found", - ); - unreachable!() - }; - options.terminated = true; - return vec![format!(" br label %{}", label)]; - } - NodeType::LogicalAnd | NodeType::LogicalOr => { - let mut lines = vec![]; - let predicate = self.gen_predicate(node.lhs.as_ref().unwrap(), options, &mut lines); - let slot = options.expression_slots[&node.token.as_ref().unwrap().pos]; - let rhs_label = options.new_label(); - let short_label = options.new_label(); + NodeType::DoWhile => { + let body_label = options.new_label(); + let condition_label = format!("continue.{}", node.token.as_ref().unwrap().pos); let end_label = options.new_label(); - let is_and = matches!(node.nt, NodeType::LogicalAnd); + let mut lines = vec![ + format!(" br label %{}", body_label), + format!("{}:", body_label), + ]; + options.break_labels.push(end_label.clone()); + lines.extend(self.gen_node(node.then.as_ref().unwrap(), options)); + options.break_labels.pop(); + if !options.terminated { + lines.push(format!(" br label %{}", condition_label)); + } + options.terminated = false; + lines.push(format!("{}:", condition_label)); + let predicate = + self.gen_predicate(node.cond.as_ref().unwrap(), options, &mut lines); lines.push(format!( " br i1 {}, label %{}, label %{}", - predicate, - if is_and { &rhs_label } else { &short_label }, - if is_and { &short_label } else { &rhs_label } - )); - lines.push(format!("{}:", short_label)); - lines.push(format!( - " store i32 {}, ptr %{}, align 4", - if is_and { 0 } else { 1 }, - slot + predicate, body_label, end_label )); - lines.push(format!(" br label %{}", end_label)); - lines.push(format!("{}:", rhs_label)); - let predicate = self.gen_predicate(node.rhs.as_ref().unwrap(), options, &mut lines); - let value = options.new_temporary(); - lines.push(format!(" %{} = zext i1 {} to i32", value, predicate)); - lines.push(format!(" store i32 %{}, ptr %{}, align 4", value, slot)); - lines.push(format!(" br label %{}", end_label)); lines.push(format!("{}:", end_label)); - lines.push(format!( - " %{} = load i32, ptr %{}, align 4", - options.new_register(), - slot - )); return lines; } - NodeType::Conditional => { - if node.resolve_type_view().as_deref() == Some(&Type::Void) { - let mut lines = vec![]; - let predicate = - self.gen_predicate(node.cond.as_ref().unwrap(), options, &mut lines); - let then_label = options.new_label(); - let else_label = options.new_label(); - let end_label = options.new_label(); - lines.push(format!( - " br i1 {}, label %{}, label %{}", - predicate, then_label, else_label - )); - for (label, branch) in [ - (&then_label, node.then.as_ref().unwrap()), - (&else_label, node.els.as_ref().unwrap()), - ] { - lines.push(format!("{}:", label)); - lines.extend(self.gen_node(branch, options)); - options.register_queue.pop(); - lines.push(format!(" br label %{}", end_label)); - } - lines.push(format!("{}:", end_label)); - options.register_queue.push("0".to_string()); - return lines; - } - - let mut lines = vec![]; - let ty = node.resolve_type_view().unwrap(); - let slot = options.expression_slots[&node.token.as_ref().unwrap().pos]; - let predicate = - self.gen_predicate(node.cond.as_ref().unwrap(), options, &mut lines); - let then_label = options.new_label(); - let else_label = options.new_label(); - let end_label = options.new_label(); + NodeType::Switch => { + let mut lines = self.gen_node(node.cond.as_ref().unwrap(), options); + let value = options.register_queue.pop().unwrap(); + let selector_ty = node + .cond + .as_ref() + .unwrap() + .resolve_type_view() + .unwrap() + .promoted(); + let value = Self::convert_integer( + value, + &node.cond.as_ref().unwrap().resolve_type_view().unwrap(), + &selector_ty, + options, + &mut lines, + ); + let end = options.new_label(); + let default = node + .children + .iter() + .find(|case| case.value.is_none()) + .map(|case| format!("case.{}", case.token.as_ref().unwrap().pos)) + .unwrap_or(end.clone()); lines.push(format!( - " br i1 {}, label %{}, label %{}", - predicate, then_label, else_label + " switch {} {}, label %{} [", + Self::gen_type(&selector_ty), + value, + default )); - for (label, branch) in [ - (&then_label, node.then.as_ref().unwrap()), - (&else_label, node.els.as_ref().unwrap()), - ] { - lines.push(format!("{}:", label)); - lines.extend(self.gen_node(branch, options)); - let value = options.register_queue.pop().unwrap(); - let value = Self::convert_integer( - value, - &branch.resolve_type_view().unwrap(), - &ty, - options, - &mut lines, + for case in &node.children { + if let Some(value) = case.value { + lines.push(format!( + " {} {}, label %case.{}", + Self::gen_type(&selector_ty), + value as i64, + case.token.as_ref().unwrap().pos + )); + } + } + lines.push(" ]".to_string()); + options.terminated = true; + options.break_labels.push(end.clone()); + lines.extend(self.gen_node(node.then.as_ref().unwrap(), options)); + options.break_labels.pop(); + if !options.terminated { + lines.push(format!(" br label %{}", end)); + } + lines.push(format!("{}:", end)); + options.terminated = false; + return lines; + } + NodeType::Case => { + let label = format!("case.{}", node.token.as_ref().unwrap().pos); + let mut lines = vec![]; + if !options.terminated { + lines.push(format!(" br label %{}", label)); + } + lines.push(format!("{}:", label)); + options.terminated = false; + lines.extend(self.gen_node(node.lhs.as_ref().unwrap(), options)); + return lines; + } + NodeType::Break => { + let Some(label) = options.break_labels.last() else { + self.error_logger.print_error_position( + node.token.as_ref().unwrap().pos, + "unexpected break found", ); - lines.push(format!( - " store {} {}, ptr %{}, align {}", - Self::gen_type(&ty), - value, - slot, - Self::align(&ty) - )); - lines.push(format!(" br label %{}", end_label)); + unreachable!() + }; + options.terminated = true; + return vec![format!(" br label %{}", label)]; + } + NodeType::Continue => { + options.terminated = true; + return vec![format!(" br label %continue.{}", node.value.unwrap())]; + } + NodeType::Goto => { + options.terminated = true; + return vec![format!(" br label %goto.{}", node.value.unwrap())]; + } + NodeType::Label => { + let label = format!("goto.{}", node.value.unwrap()); + let mut lines = vec![]; + if !options.terminated { + lines.push(format!(" br label %{label}")); } - lines.push(format!("{}:", end_label)); - lines.push(format!( - " %{} = load {}, ptr %{}, align {}", - options.new_register(), - Self::gen_type(&ty), - slot, - Self::align(&ty) - )); + lines.push(format!("{label}:")); + options.terminated = false; + lines.extend(self.gen_node(node.lhs.as_ref().unwrap(), options)); return lines; } - NodeType::SuffixIncr | NodeType::SuffixDecr => { - let operand = node.lhs.as_ref().unwrap(); - let mut lines = self.gen_addr(operand, options); - let address = options.register_queue.pop().unwrap(); - let ty = operand.resolve_type_view().unwrap(); - let old = options.new_temporary(); + NodeType::FloatAbs => { + let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); + let value = options.register_queue.pop().unwrap(); + let ty = node.resolve_type_view().unwrap(); + let bits = ty.float_bits().unwrap(); + let float = Self::gen_type(&ty); + let raw = options.new_temporary(); + let masked = options.new_temporary(); + let result = options.new_register(); + let mask = (1u128 << (bits - 1)) - 1; + lines.extend([ + format!(" %{raw} = bitcast {float} {value} to i{bits}"), + format!(" %{masked} = and i{bits} %{raw}, {mask}"), + format!(" %{result} = bitcast i{bits} %{masked} to {float}"), + ]); + return lines; + } + NodeType::BitScan { bits, leading } => { + let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); + let operand = options.register_queue.pop().unwrap(); + let count = options.new_temporary(); + let intrinsic = if leading { "ctlz" } else { "cttz" }; lines.push(format!( - " %{} = load {}{}, ptr {}, align {}", - old, - if node.volatile_access { - "volatile " - } else { - "" - }, - Self::gen_type(&ty), - address, - Self::access_alignment(operand) + " %{count} = call i{bits} @llvm.{intrinsic}.i{bits}(i{bits} {operand}, i1 false)" )); - let new = options.new_temporary(); - if let Type::Ptr(child) = ty.as_ref() { - lines.push(format!( - " %{} = getelementptr inbounds {}, ptr %{}, i32 {}", - new, - Self::gen_type(child), - old, - if matches!(node.nt, NodeType::SuffixIncr) { - 1 - } else { - -1 - } - )); + if bits == 64 { + let result = options.new_register(); + lines.push(format!(" %{result} = trunc i64 %{count} to i32")); } else { - lines.push(format!( - " %{} = {} {} %{}, {}", - new, - if matches!(node.nt, NodeType::SuffixIncr) { - if ty.float_bits().is_some() { - "fadd" - } else { - "add" - } - } else if ty.float_bits().is_some() { - "fsub" - } else { - "sub" - }, - Self::gen_type(&ty), - old, - if ty.float_bits().is_some() { - Self::float_literal(&ty, ty.float_constant(1.0)) - } else { - "1".to_string() - } - )); + options.register_queue.push(format!("%{count}")); } - let new = if *ty == Type::Bool { - let predicate = options.new_temporary(); - lines.push(format!(" %{predicate} = icmp ne i8 %{new}, 0")); - let result = options.new_temporary(); - lines.push(format!(" %{result} = zext i1 %{predicate} to i8")); - result + return lines; + } + NodeType::VaStart | NodeType::VaEnd => { + let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); + let pointer = options.register_queue.pop().unwrap(); + let intrinsic = if node.nt == NodeType::VaStart { + "va_start" } else { - new + "va_end" }; + lines.push(format!(" call void @llvm.{intrinsic}(ptr {pointer})")); + options.register_queue.push("0".into()); + return lines; + } + NodeType::StackAlloc => { + let mut lines = self.gen_node(node.lhs.as_ref().unwrap(), options); + let size = options.register_queue.pop().unwrap(); + let register = options.new_register(); + lines.push(format!(" %{register} = alloca i8, i64 {size}, align 16")); + return lines; + } + NodeType::Prefetch(write, locality) => { + return self.gen_prefetch(node, write, locality, options); + } + NodeType::Atomic(operation) => { + return self.gen_atomic(node, operation, options); + } + NodeType::InlineAsm(volatile, memory) => { + return self.gen_inline_assembly(node, volatile, memory, options); + } + NodeType::ZeroFill { bytes } => { + let mut lines = self.gen_addr(node.lhs.as_ref().unwrap(), options); + let pointer = options.register_queue.pop().unwrap(); lines.push(format!( - " store {}{} %{}, ptr {}, align {}", - if node.volatile_access { - "volatile " - } else { - "" - }, - Self::gen_type(&ty), - new, - address, - Self::access_alignment(operand) + " call void @llvm.memset.p0.i64(ptr align 1 {pointer}, i8 0, i64 {bytes}, i1 false)" )); - options.register_queue.push(format!("%{}", old)); + options.register_queue.push("0".into()); return lines; } - NodeType::If => { - let mut lines = vec![]; - let predicate = - self.gen_predicate(node.cond.as_ref().unwrap(), options, &mut lines); - let then_label = options.new_label(); - let else_label = options.new_label(); - let end_label = options.new_label(); + NodeType::CopyBytes { bytes } => { + let mut lines = self.gen_addr(node.lhs.as_ref().unwrap(), options); + let destination = options.register_queue.pop().unwrap(); + lines.extend(self.gen_addr(node.rhs.as_ref().unwrap(), options)); + let source = options.register_queue.pop().unwrap(); lines.push(format!( - " br i1 {}, label %{}, label %{}", - predicate, then_label, else_label, + " call void @llvm.memcpy.p0.p0.i64(ptr align 1 {destination}, ptr align 1 {source}, i64 {bytes}, i1 false)" )); - - lines.push(format!("{}:", then_label)); - options.terminated = false; - lines.extend(self.gen_node(node.then.as_ref().unwrap(), options)); - let then_terminated = options.terminated; - if !then_terminated { - lines.push(format!(" br label %{}", end_label)); - } - - lines.push(format!("{}:", else_label)); - options.terminated = false; - if let Some(els) = &node.els { - lines.extend(self.gen_node(els, options)); - } - let else_terminated = options.terminated; - if !else_terminated { - lines.push(format!(" br label %{}", end_label)); - } - - options.terminated = then_terminated && else_terminated; - if !options.terminated { - lines.push(format!("{}:", end_label)); - } + options.register_queue.push(destination); return lines; } _ => {} @@ -1539,15 +1536,15 @@ impl<'a> IrGenerator<'a> { NodeType::Mul => "mul nsw", NodeType::Div => "sdiv", NodeType::Mod => "srem", + NodeType::BitLeft => "shl", + NodeType::BitRight => "ashr", + NodeType::BitAnd => "and", + NodeType::BitXor => "xor", + NodeType::BitOr => "or", NodeType::Eq => "icmp eq", NodeType::Ne => "icmp ne", NodeType::Lt => "icmp slt", NodeType::Le => "icmp sle", - NodeType::BitAnd => "and", - NodeType::BitXor => "xor", - NodeType::BitOr => "or", - NodeType::BitLeft => "shl", - NodeType::BitRight => "ashr", _ => "unknown", }; let mut rhs_register = options.register_queue.pop().unwrap(); @@ -1584,14 +1581,14 @@ impl<'a> IrGenerator<'a> { } } else if operand_ty.integer_info().is_some_and(|(_, signed)| !signed) { match node.nt { + NodeType::Add => "add", + NodeType::Sub => "sub", + NodeType::Mul => "mul", NodeType::Div => "udiv", NodeType::Mod => "urem", + NodeType::BitRight => "lshr", NodeType::Lt => "icmp ult", NodeType::Le => "icmp ule", - NodeType::BitRight => "lshr", - NodeType::Add => "add", - NodeType::Sub => "sub", - NodeType::Mul => "mul", _ => operation, } } else { @@ -1626,15 +1623,15 @@ impl<'a> IrGenerator<'a> { | NodeType::Mul | NodeType::Div | NodeType::Mod - | NodeType::Eq - | NodeType::Ne - | NodeType::Lt - | NodeType::Le + | NodeType::BitLeft + | NodeType::BitRight | NodeType::BitAnd | NodeType::BitXor | NodeType::BitOr - | NodeType::BitLeft - | NodeType::BitRight => { + | NodeType::Eq + | NodeType::Ne + | NodeType::Lt + | NodeType::Le => { format!( " %{} = {} {} {}, {}", options.new_register(), diff --git a/src/generator/llvm/generator/aggregate.rs b/src/generator/llvm/generator/aggregate.rs index b802fab..f5aca91 100644 --- a/src/generator/llvm/generator/aggregate.rs +++ b/src/generator/llvm/generator/aggregate.rs @@ -220,16 +220,16 @@ impl IrGenerator<'_> { let mut registers = HashMap::new(); let mut queue = vec![]; let mut options = Options { + return_ty: result.clone(), + return_pointer: signature.indirect_result.then(|| "%abi.result".into()), register_number: &mut number, register_map: &mut registers, register_queue: &mut queue, - label_number: 0, - terminated: false, - return_ty: result.clone(), - return_pointer: signature.indirect_result.then(|| "%abi.result".into()), - break_labels: vec![], expression_slots: HashMap::new(), argument_slots: HashMap::new(), + label_number: 0, + break_labels: vec![], + terminated: false, }; let mut lines = vec![format!( "define {}{}{return_abi} {}({}){} uwtable \"frame-pointer\"=\"all\" {{", diff --git a/src/generator/llvm/generator/globals.rs b/src/generator/llvm/generator/globals.rs index 676f8bf..4759ca5 100644 --- a/src/generator/llvm/generator/globals.rs +++ b/src/generator/llvm/generator/globals.rs @@ -88,6 +88,7 @@ impl IrGenerator<'_> { pub(super) fn contains_packed_record(ty: &Type) -> bool { match ty.unqualified() { + Type::Arr(child, _) => Self::contains_packed_record(child), Type::Struct(record) => { record.definition().borrow().packing.is_some() || ty.contains_typedef_alignment() @@ -96,7 +97,6 @@ impl IrGenerator<'_> { .iter() .any(|member| Self::contains_packed_record(&member.ty)) } - Type::Arr(child, _) => Self::contains_packed_record(child), _ => false, } } @@ -182,11 +182,26 @@ impl IrGenerator<'_> { } } match ty { - Type::Aligned(child, _) => self.gen_initializer_element(child, data), - Type::Vla(..) => unreachable!("variable length arrays have automatic storage"), + Type::Void | Type::Func(_, _) | Type::VariadicFunc(_, _) => { + format!("{} zeroinitializer", Self::gen_type(ty)) + } + Type::Bool + | Type::I8 + | Type::SignedChar + | Type::I32 + | Type::Integer { .. } + | Type::LongLong { .. } => format!( + "{} {}", + Self::gen_type(ty), + if let Some(GlobalVariableData::Elem(s)) = data { + s + } else { + "0" + } + ), Type::Integer128 { .. } => "<2 x i64> zeroinitializer".into(), + Type::Enum(value) => self.gen_initializer_element(&value.underlying.borrow(), data), Type::Float16 => "half zeroinitializer".into(), - Type::Vector(..) => format!("{} zeroinitializer", Self::gen_type(ty)), Type::LongDouble(80) => { let bits = if let Some(GlobalVariableData::Elem(value)) = data { value.parse::().unwrap() @@ -206,7 +221,42 @@ impl IrGenerator<'_> { }; format!("{} {}", Self::gen_type(ty), Self::float_literal(ty, bits)) } - + Type::Ptr(_) => format!( + "ptr {}", + match data { + Some(GlobalVariableData::Address { symbol, offset }) => { + if *offset == 0 { + self.symbol_name(symbol) + } else { + format!( + "getelementptr (i8, ptr {}, i64 {offset})", + self.symbol_name(symbol) + ) + } + } + Some(GlobalVariableData::Elem(value)) if value.starts_with('@') => + value.clone(), + Some(GlobalVariableData::Elem(value)) if value != "0" => + format!("inttoptr (i64 {} to ptr)", value), + _ => "null".to_string(), + } + ), + Type::Arr(children_ty, size) => { + if let Some(GlobalVariableData::Arr(v)) = data { + format!( + "{} [{}]", + Self::gen_type(ty), + (0..size.unwrap_or(0)) + .map(|i| self.gen_initializer_element(children_ty, v.get(i))) + .collect::>() + .join(", "), + ) + } else { + format!("{} zeroinitializer", Self::gen_type(ty)) + } + } + Type::Vla(..) => unreachable!("variable length arrays have automatic storage"), + Type::Vector(..) => format!("{} zeroinitializer", Self::gen_type(ty)), Type::Struct(struct_type) if struct_type.definition().borrow().is_union || struct_type.has_bit_fields() => { @@ -249,7 +299,6 @@ impl IrGenerator<'_> { format!("{} [{}]", Self::gen_type(ty), elements) } } - Type::Qualified(child, _) => self.gen_initializer_element(child, data), Type::Struct(struct_type) => { if let Some(GlobalVariableData::Arr(elements)) = data { let mut fields = vec![]; @@ -276,58 +325,8 @@ impl IrGenerator<'_> { format!("{} zeroinitializer", Self::gen_type(ty)) } } - Type::Arr(children_ty, size) => { - if let Some(GlobalVariableData::Arr(v)) = data { - format!( - "{} [{}]", - Self::gen_type(ty), - (0..size.unwrap_or(0)) - .map(|i| self.gen_initializer_element(children_ty, v.get(i))) - .collect::>() - .join(", "), - ) - } else { - format!("{} zeroinitializer", Self::gen_type(ty)) - } - } - Type::Ptr(_) => format!( - "ptr {}", - match data { - Some(GlobalVariableData::Address { symbol, offset }) => { - if *offset == 0 { - self.symbol_name(symbol) - } else { - format!( - "getelementptr (i8, ptr {}, i64 {offset})", - self.symbol_name(symbol) - ) - } - } - Some(GlobalVariableData::Elem(value)) if value.starts_with('@') => - value.clone(), - Some(GlobalVariableData::Elem(value)) if value != "0" => - format!("inttoptr (i64 {} to ptr)", value), - _ => "null".to_string(), - } - ), - Type::Enum(value) => self.gen_initializer_element(&value.underlying.borrow(), data), - Type::I8 - | Type::SignedChar - | Type::I32 - | Type::LongLong { .. } - | Type::Integer { .. } - | Type::Bool => format!( - "{} {}", - Self::gen_type(ty), - if let Some(GlobalVariableData::Elem(s)) = data { - s - } else { - "0" - } - ), - Type::Void | Type::Func(_, _) | Type::VariadicFunc(_, _) => { - format!("{} zeroinitializer", Self::gen_type(ty)) - } + Type::Qualified(child, _) => self.gen_initializer_element(child, data), + Type::Aligned(child, _) => self.gen_initializer_element(child, data), } } } diff --git a/src/generator/mod.rs b/src/generator/mod.rs index 2724dde..6f02b41 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -73,10 +73,10 @@ pub(crate) fn referenced_symbols<'a>( pending: &mut Vec<&'a str>, ) { match data { - GlobalVariableData::Address { symbol, .. } => add_name(symbol, names, pending), GlobalVariableData::Elem(value) if value.starts_with('@') => { add_name(&value[1..], names, pending) } + GlobalVariableData::Address { symbol, .. } => add_name(symbol, names, pending), GlobalVariableData::Arr(values) => { for value in values { data_names(value, names, pending); diff --git a/src/generator/x86_64/generator.rs b/src/generator/x86_64/generator.rs index 14ee983..e5f8285 100644 --- a/src/generator/x86_64/generator.rs +++ b/src/generator/x86_64/generator.rs @@ -58,7 +58,6 @@ impl<'a> AsmGenerator<'a> { let generator = Self { error_logger: self.error_logger, target_os: self.target_os, - symbol_names: ast.symbol_names.clone(), defined_symbols: ast .functions .iter() @@ -71,6 +70,7 @@ impl<'a> AsmGenerator<'a> { .map(|(name, _)| name.clone()), ) .collect(), + symbol_names: ast.symbol_names.clone(), }; let references = if ast.functions.values().any(|function| { @@ -557,102 +557,60 @@ impl<'a> AsmGenerator<'a> { ); } match node.nt { - NodeType::Prefetch(_, locality) => { - return self.gen_prefetch(node, locality, offset, breakable_branch_num); - } - NodeType::InlineAsm(_, _) => { - return self.gen_inline_assembly(node, offset, breakable_branch_num); - } - NodeType::Atomic(operation) => { - return self.gen_atomic(node, operation, offset, breakable_branch_num); - } - NodeType::VaStart | NodeType::VaEnd => { - let mut lines = vec![ - self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, R11), - ]; - if node.nt == NodeType::VaStart { - let (gp, fp, stack) = crate::generator::abi::variadic_state( - node, - crate::generator::Arch::X86_64, - self.target_os, - ); - lines.extend([ - Assembly::inst2(MOV, EAX, gp * 8), - Assembly::inst2(MOV, PtrOffset(R11, 0, 4), EAX), - Assembly::inst2(MOV, EAX, 48 + fp * 16), - Assembly::inst2(MOV, PtrOffset(R11, 4, 4), EAX), - Assembly::inst2(LEA, RAX, PtrOffset(RBP, 16 + stack, 8)), - Assembly::inst2(MOV, PtrOffset(R11, 8, 8), RAX), - Assembly::inst2(MOV, RAX, RBP), - Assembly::inst2(SUB, RAX, offset), - Assembly::inst2(MOV, PtrOffset(R11, 16, 8), RAX), - ]); - } - lines.push(Assembly::inst1(PUSH, 0)); - return lines.into(); - } - - NodeType::CopyBytes { bytes } => { - return vec![ - self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - self.gen_addr(node.rhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RSI), - Assembly::inst1(POP, RDI), - Assembly::inst1(PUSH, RDI), - Assembly::inst2(MOV, RCX, bytes), - Assembly::inst0(REPMOVSB), - ] - .into(); + NodeType::Num => { + let value = node.value.unwrap(); + return if value <= i32::MAX as usize { + Assembly::inst1(PUSH, value) + } else { + vec![Assembly::inst2(MOV, RAX, value), Assembly::inst1(PUSH, RAX)].into() + }; } - NodeType::ZeroFill { bytes } => { + NodeType::LocalVar | NodeType::GlobalVar => { return vec![ - self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RDI), - Assembly::inst2(MOV, RCX, bytes), - Assembly::inst2(XOR, EAX, EAX), - Assembly::inst0(REPSTOSB), - Assembly::inst1(PUSH, 0), + self.gen_addr(node, offset, breakable_branch_num), + if let Some( + Type::Arr(..) + | Type::Vla(..) + | Type::Struct(_) + | Type::Func(..) + | Type::VariadicFunc(..), + ) = node.resolve_type() + { + vec![].into() + } else { + vec![ + Assembly::inst1(POP, RAX), + self.deref_rax(node), + Assembly::inst1(PUSH, RAX), + ] + .into() + }, ] .into(); } - NodeType::BitScan { bits, leading } => { - let (input, count) = if bits == 32 { (EAX, ECX) } else { (RAX, RCX) }; - let mut lines = vec![ - self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RAX), - Assembly::inst2(if leading { BSR } else { BSF }, count, input), - Assembly::inst2( - MOV, - R11, - if leading { - usize::MAX - } else { - usize::from(bits) - }, - ), - // BSF/BSR leave the destination undefined for zero; select a fallback first. - Assembly::inst2(CMOVE, RCX, R11), - ]; - if leading { - lines.extend([ - Assembly::inst2(MOV, RAX, usize::from(bits - 1)), - Assembly::inst2(SUB, RAX, RCX), - ]); - } else { - lines.push(Assembly::inst2(MOV, RAX, RCX)); - } - lines.push(Assembly::inst1(PUSH, RAX)); - return lines.into(); + NodeType::Addr => { + return self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num); } - NodeType::FloatAbs => { - let shift = 65 - node.resolve_type().unwrap().float_bits().unwrap() as usize; + NodeType::Deref => { return vec![ self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RAX), - Assembly::inst2(SHL, RAX, shift), - Assembly::inst2(SHR, RAX, shift), - Assembly::inst1(PUSH, RAX), + if let Some( + Type::Arr(..) + | Type::Vla(..) + | Type::Struct(_) + | Type::Func(..) + | Type::VariadicFunc(..), + ) = node.lhs.as_ref().unwrap().dest_type() + { + vec![].into() + } else { + vec![ + Assembly::inst1(POP, RAX), + self.deref_rax(node), + Assembly::inst1(PUSH, RAX), + ] + .into() + }, ] .into(); } @@ -674,110 +632,95 @@ impl<'a> AsmGenerator<'a> { lines.push(Assembly::inst1(PUSH, RAX)); return lines.into(); } - NodeType::Sequence => { - let mut lines = vec![]; - for (index, child) in node.children.iter().enumerate() { - lines.push(self.gen_node(child, offset, breakable_branch_num)); - if index + 1 < node.children.len() - && !matches!(child.nt, NodeType::Block | NodeType::DefVar) - { - lines.push(Assembly::inst1(POP, RAX)); - } - } - return lines.into(); - } - NodeType::DefVar => { - return self.gen_statements(&node.children, offset, breakable_branch_num); - } - NodeType::CallFunc => { - if matches!(node.resolve_type(), Some(Type::Struct(_))) - || node - .args - .iter() - .any(|arg| matches!(arg.resolve_type(), Some(Type::Struct(_)))) - { - return self.aggregate_call(node, offset, breakable_branch_num); - } - if node.resolve_type().unwrap().float_bits().is_some() - || node - .args - .iter() - .any(|arg| arg.resolve_type().unwrap().float_bits().is_some()) - { - return self.float_call(node, offset, breakable_branch_num); - } - let stack_size = node.args.len().saturating_sub(ARGS_REG.len()) * 8; + NodeType::BitLeft | NodeType::BitRight => { return vec![ - node.lhs - .as_ref() - .map(|callee| self.gen_node(callee, offset, breakable_branch_num)) - .unwrap_or_else(|| vec![].into()), - Assembly::inst2(MOV, RAX, RSP), - Assembly::inst2(ADD, RAX, 8), - Assembly::inst2(SUB, RAX, node.args.len().saturating_sub(ARGS_REG.len()) * 8), - Assembly::inst2(MOV, RDI, 16), - Assembly::inst0(CQO), - Assembly::inst1(IDIV, RDI), - Assembly::inst2(SUB, RSP, RDX), - Assembly::inst1(PUSH, RDX), - node.args - .iter() - .map(|node| self.gen_node(node, offset, breakable_branch_num)) - .collect::>() - .into(), - ARGS_REG - .iter() - .take(node.args.len()) - .map(|op| Assembly::inst1(POP, *op)) - .collect::>() - .into(), - Assembly::inst2(MOV, RAX, 0), - if node.lhs.is_some() { - vec![ - // The saved callee sits above the alignment padding. - Assembly::inst2(MOV, R10, PtrAdd(RSP, format!("{stack_size}"))), - Assembly::inst2(ADD, R10, stack_size + 8), - Assembly::inst2(ADD, R10, RSP), - Assembly::inst2(MOV, R10, Ptr(R10, 8)), - Assembly::inst1(CALL, R10), - ] - .into() - } else { - Assembly::inst1(CALL, self.with_prefix(&node.global_name)) - }, - Assembly::inst2(ADD, RSP, node.args.len().saturating_sub(ARGS_REG.len()) * 8), - Assembly::inst1(POP, RDI), - Assembly::inst2(ADD, RSP, RDI), - if node.lhs.is_some() { - Assembly::inst2(ADD, RSP, 8) - } else { - vec![].into() - }, - node.cty - .as_ref() - .map(|ty| Self::normalize_integer(ty, RAX)) - .unwrap_or_else(|| vec![].into()), + self.gen_node(node.rhs.as_ref().unwrap(), offset, breakable_branch_num), + self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RAX), + Assembly::inst1(POP, RCX), + Assembly::inst2( + match node.nt { + NodeType::BitLeft => SHL, + NodeType::BitRight => { + if node.resolve_type().unwrap().integer_info().unwrap().1 { + SAR + } else { + SHR + } + } + _ => { + unreachable!() + } + }, + RAX, + CL, + ), + Self::normalize_integer(&node.resolve_type().unwrap(), RAX), Assembly::inst1(PUSH, RAX), ] .into(); } - NodeType::If | NodeType::Conditional => { - let branch_num = node.token.as_ref().unwrap().pos; - let branch = |child: &Node| { - if node.nt == NodeType::If { - self.gen_statements( - std::slice::from_ref(child), - offset, - breakable_branch_num, - ) - } else { - self.gen_node(child, offset, breakable_branch_num) - } - }; + NodeType::BitNot => { return vec![ - self.gen_node(node.cond.as_ref().unwrap(), offset, breakable_branch_num), + self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), Assembly::inst1(POP, RAX), - Assembly::inst2(CMP, RAX, 0), + Assembly::inst1(NOT, RAX), + Self::normalize_integer(&node.resolve_type().unwrap(), RAX), + Assembly::inst1(PUSH, RAX), + ] + .into(); + } + NodeType::LogicalAnd => { + let branch_num = node.token.as_ref().unwrap().pos; + return vec![ + self.gen_node(node.lhs.as_ref().unwrap(), offset, branch_num), + Assembly::inst1(POP, RAX), + Assembly::inst2(CMP, RAX, 0), + Assembly::inst1(JE, EndFlag(branch_num)), + self.gen_node(node.rhs.as_ref().unwrap(), offset, branch_num), + Assembly::inst1(POP, RAX), + format!("{}:", EndFlag(branch_num)).into(), + Assembly::inst2(CMP, RAX, 0), + Assembly::inst1(SETNE, AL), + Assembly::inst2(MOVZX, RAX, AL), + Assembly::inst1(PUSH, RAX), + ] + .into(); + } + NodeType::LogicalOr => { + let branch_num = node.token.as_ref().unwrap().pos; + return vec![ + self.gen_node(node.lhs.as_ref().unwrap(), offset, branch_num), + Assembly::inst1(POP, RAX), + Assembly::inst2(CMP, RAX, 0), + Assembly::inst1(JNE, EndFlag(branch_num)), + self.gen_node(node.rhs.as_ref().unwrap(), offset, branch_num), + Assembly::inst1(POP, RAX), + format!("{}:", EndFlag(branch_num)).into(), + Assembly::inst2(CMP, RAX, 0), + Assembly::inst1(SETNE, AL), + Assembly::inst2(MOVZX, RAX, AL), + Assembly::inst1(PUSH, RAX), + ] + .into(); + } + NodeType::Conditional | NodeType::If => { + let branch_num = node.token.as_ref().unwrap().pos; + let branch = |child: &Node| { + if node.nt == NodeType::If { + self.gen_statements( + std::slice::from_ref(child), + offset, + breakable_branch_num, + ) + } else { + self.gen_node(child, offset, breakable_branch_num) + } + }; + return vec![ + self.gen_node(node.cond.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RAX), + Assembly::inst2(CMP, RAX, 0), Assembly::inst1(JE, ElseFlag(branch_num)), branch(node.then.as_deref().unwrap()), Assembly::inst1(JMP, EndFlag(branch_num)), @@ -804,68 +747,164 @@ impl<'a> AsmGenerator<'a> { ] .into(); } - NodeType::Switch => { - let branch_num = node.token.as_ref().unwrap().pos; - let mut lines = vec![ - self.gen_node(node.cond.as_ref().unwrap(), offset, breakable_branch_num), + NodeType::Assign => { + return vec![ + self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), + self.gen_node(node.rhs.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RDI), Assembly::inst1(POP, RAX), - ]; - let mut default = format!("{}", EndFlag(branch_num)); - for case in &node.children { - let label = format!(".Lcase.{}", case.token.as_ref().unwrap().pos); - if let Some(value) = case.value { - lines.push(Assembly::inst2(MOV, RDI, value)); - lines.push(Assembly::inst2(CMP, RAX, RDI)); - lines.push(Assembly::inst1(JE, label)); - } else { - default = label; - } - } - lines.push(Assembly::inst1(JMP, default)); - lines.push(self.gen_node(node.then.as_ref().unwrap(), offset, branch_num)); - lines.push(format!("{}:", EndFlag(branch_num)).into()); - return lines.into(); - } - NodeType::Goto => { - return Assembly::inst1(JMP, format!(".Lgoto.{}", node.value.unwrap())); + self.operation2rdi(node.lhs.as_ref().unwrap().resolve_type(), MOV, RAX), + self.deref_rax(node.lhs.as_ref().unwrap()), + Assembly::inst1(PUSH, RAX), + ] + .into(); } - NodeType::Label => { + NodeType::SuffixIncr | NodeType::SuffixDecr => { + let op = if let NodeType::SuffixIncr = node.nt { + ADD + } else { + SUB + }; + if node.resolve_type() == Some(Type::Bool) { + return vec![ + self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RAX), + Assembly::inst2(MOV, RDX, RAX), + self.deref_rax(node.lhs.as_ref().unwrap()), + Assembly::inst2(MOV, RDI, RAX), + Assembly::inst2(op, RDI, 1), + Self::normalize_integer(&Type::Bool, RDI), + Assembly::inst2(MOV, Ptr(RDX, 1), DIL), + Assembly::inst1(PUSH, RAX), + ] + .into(); + } return vec![ - format!(".Lgoto.{}:", node.value.unwrap()).into(), - self.gen_statements( - std::slice::from_ref(node.lhs.as_deref().unwrap()), - offset, - breakable_branch_num, - ), + self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RAX), + Assembly::inst2(MOV, RDI, 1), + if let Some(t) = node.lhs.as_ref().unwrap().dest_type() { + Assembly::inst2(IMUL, RDI, t.size_of()) + } else { + vec![].into() + }, + Assembly::inst2(MOV, RDX, RAX), + self.deref_rax(node.lhs.as_ref().unwrap()), + self.operation2rdi(node.lhs.as_ref().unwrap().resolve_type(), op, RDX), + Assembly::inst1(PUSH, RAX), ] .into(); } - NodeType::Case => { + NodeType::CallFunc => { + if matches!(node.resolve_type(), Some(Type::Struct(_))) + || node + .args + .iter() + .any(|arg| matches!(arg.resolve_type(), Some(Type::Struct(_)))) + { + return self.aggregate_call(node, offset, breakable_branch_num); + } + if node.resolve_type().unwrap().float_bits().is_some() + || node + .args + .iter() + .any(|arg| arg.resolve_type().unwrap().float_bits().is_some()) + { + return self.float_call(node, offset, breakable_branch_num); + } + let stack_size = node.args.len().saturating_sub(ARGS_REG.len()) * 8; return vec![ - format!(".Lcase.{}:", node.token.as_ref().unwrap().pos).into(), - self.gen_statements( - std::slice::from_ref(node.lhs.as_deref().unwrap()), - offset, - breakable_branch_num, - ), + node.lhs + .as_ref() + .map(|callee| self.gen_node(callee, offset, breakable_branch_num)) + .unwrap_or_else(|| vec![].into()), + Assembly::inst2(MOV, RAX, RSP), + Assembly::inst2(ADD, RAX, 8), + Assembly::inst2(SUB, RAX, node.args.len().saturating_sub(ARGS_REG.len()) * 8), + Assembly::inst2(MOV, RDI, 16), + Assembly::inst0(CQO), + Assembly::inst1(IDIV, RDI), + Assembly::inst2(SUB, RSP, RDX), + Assembly::inst1(PUSH, RDX), + node.args + .iter() + .map(|node| self.gen_node(node, offset, breakable_branch_num)) + .collect::>() + .into(), + ARGS_REG + .iter() + .take(node.args.len()) + .map(|op| Assembly::inst1(POP, *op)) + .collect::>() + .into(), + Assembly::inst2(MOV, RAX, 0), + if node.lhs.is_some() { + vec![ + // The saved callee sits above the alignment padding. + Assembly::inst2(MOV, R10, PtrAdd(RSP, format!("{stack_size}"))), + Assembly::inst2(ADD, R10, stack_size + 8), + Assembly::inst2(ADD, R10, RSP), + Assembly::inst2(MOV, R10, Ptr(R10, 8)), + Assembly::inst1(CALL, R10), + ] + .into() + } else { + Assembly::inst1(CALL, self.with_prefix(&node.global_name)) + }, + Assembly::inst2(ADD, RSP, node.args.len().saturating_sub(ARGS_REG.len()) * 8), + Assembly::inst1(POP, RDI), + Assembly::inst2(ADD, RSP, RDI), + if node.lhs.is_some() { + Assembly::inst2(ADD, RSP, 8) + } else { + vec![].into() + }, + node.cty + .as_ref() + .map(|ty| Self::normalize_integer(ty, RAX)) + .unwrap_or_else(|| vec![].into()), + Assembly::inst1(PUSH, RAX), ] .into(); } - NodeType::DoWhile => { - let branch_num = node.token.as_ref().unwrap().pos; + NodeType::Sequence => { + let mut lines = vec![]; + for (index, child) in node.children.iter().enumerate() { + lines.push(self.gen_node(child, offset, breakable_branch_num)); + if index + 1 < node.children.len() + && !matches!(child.nt, NodeType::DefVar | NodeType::Block) + { + lines.push(Assembly::inst1(POP, RAX)); + } + } + return lines.into(); + } + NodeType::DefVar => { + return self.gen_statements(&node.children, offset, breakable_branch_num); + } + NodeType::Block => { + return self.gen_statements(&node.children, offset, breakable_branch_num); + } + NodeType::Return => { + if matches!(node.resolve_type(), Some(Type::Struct(_))) { + return self.aggregate_return(node, offset, breakable_branch_num); + } + if node.lhs.is_none() { + return Assembly::epilogue(); + } return vec![ - format!("{}:", BeginFlag(branch_num)).into(), - self.gen_statements( - std::slice::from_ref(node.then.as_deref().unwrap()), - offset, - branch_num, - ), - format!(".Lcontinue.{}:", branch_num).into(), - self.gen_node(node.cond.as_ref().unwrap(), offset, branch_num), + self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), Assembly::inst1(POP, RAX), - Assembly::inst2(CMP, RAX, 0), - Assembly::inst1(JNE, BeginFlag(branch_num)), - format!("{}:", EndFlag(branch_num)).into(), + if let Some(bits) = node.resolve_type().and_then(|ty| ty.float_bits()) { + Assembly::inst2( + if bits == 32 { MOVD } else { MOVQ }, + Xmm(0), + if bits == 32 { EAX } else { RAX }, + ) + } else { + vec![].into() + }, + Assembly::epilogue(), ] .into(); } @@ -888,6 +927,24 @@ impl<'a> AsmGenerator<'a> { ]; return v.into(); } + NodeType::DoWhile => { + let branch_num = node.token.as_ref().unwrap().pos; + return vec![ + format!("{}:", BeginFlag(branch_num)).into(), + self.gen_statements( + std::slice::from_ref(node.then.as_deref().unwrap()), + offset, + branch_num, + ), + format!(".Lcontinue.{}:", branch_num).into(), + self.gen_node(node.cond.as_ref().unwrap(), offset, branch_num), + Assembly::inst1(POP, RAX), + Assembly::inst2(CMP, RAX, 0), + Assembly::inst1(JNE, BeginFlag(branch_num)), + format!("{}:", EndFlag(branch_num)).into(), + ] + .into(); + } NodeType::For => { let branch_num = node.token.as_ref().unwrap().pos; let v = vec![ @@ -924,11 +981,38 @@ impl<'a> AsmGenerator<'a> { ]; return v.into(); } - NodeType::Block => { - return self.gen_statements(&node.children, offset, breakable_branch_num); + NodeType::Switch => { + let branch_num = node.token.as_ref().unwrap().pos; + let mut lines = vec![ + self.gen_node(node.cond.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RAX), + ]; + let mut default = format!("{}", EndFlag(branch_num)); + for case in &node.children { + let label = format!(".Lcase.{}", case.token.as_ref().unwrap().pos); + if let Some(value) = case.value { + lines.push(Assembly::inst2(MOV, RDI, value)); + lines.push(Assembly::inst2(CMP, RAX, RDI)); + lines.push(Assembly::inst1(JE, label)); + } else { + default = label; + } + } + lines.push(Assembly::inst1(JMP, default)); + lines.push(self.gen_node(node.then.as_ref().unwrap(), offset, branch_num)); + lines.push(format!("{}:", EndFlag(branch_num)).into()); + return lines.into(); } - NodeType::Continue => { - return Assembly::inst1(JMP, format!(".Lcontinue.{}", node.value.unwrap())); + NodeType::Case => { + return vec![ + format!(".Lcase.{}:", node.token.as_ref().unwrap().pos).into(), + self.gen_statements( + std::slice::from_ref(node.lhs.as_deref().unwrap()), + offset, + breakable_branch_num, + ), + ] + .into(); } NodeType::Break => { if breakable_branch_num != 0 { @@ -938,205 +1022,120 @@ impl<'a> AsmGenerator<'a> { node.token.as_ref().unwrap().pos, "unexpected break found", ); - } - } - NodeType::Return => { - if matches!(node.resolve_type(), Some(Type::Struct(_))) { - return self.aggregate_return(node, offset, breakable_branch_num); - } - if node.lhs.is_none() { - return Assembly::epilogue(); - } - return vec![ - self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RAX), - if let Some(bits) = node.resolve_type().and_then(|ty| ty.float_bits()) { - Assembly::inst2( - if bits == 32 { MOVD } else { MOVQ }, - Xmm(0), - if bits == 32 { EAX } else { RAX }, - ) - } else { - vec![].into() - }, - Assembly::epilogue(), - ] - .into(); - } - NodeType::Num => { - let value = node.value.unwrap(); - return if value <= i32::MAX as usize { - Assembly::inst1(PUSH, value) - } else { - vec![Assembly::inst2(MOV, RAX, value), Assembly::inst1(PUSH, RAX)].into() - }; - } - NodeType::LocalVar | NodeType::GlobalVar => { - return vec![ - self.gen_addr(node, offset, breakable_branch_num), - if let Some( - Type::Arr(..) - | Type::Vla(..) - | Type::Struct(_) - | Type::Func(..) - | Type::VariadicFunc(..), - ) = node.resolve_type() - { - vec![].into() - } else { - vec![ - Assembly::inst1(POP, RAX), - self.deref_rax(node), - Assembly::inst1(PUSH, RAX), - ] - .into() - }, - ] - .into(); + } } - NodeType::Addr => { - return self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num); + NodeType::Continue => { + return Assembly::inst1(JMP, format!(".Lcontinue.{}", node.value.unwrap())); } - NodeType::Deref => { + NodeType::Goto => { + return Assembly::inst1(JMP, format!(".Lgoto.{}", node.value.unwrap())); + } + NodeType::Label => { return vec![ - self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - if let Some( - Type::Arr(..) - | Type::Vla(..) - | Type::Struct(_) - | Type::Func(..) - | Type::VariadicFunc(..), - ) = node.lhs.as_ref().unwrap().dest_type() - { - vec![].into() - } else { - vec![ - Assembly::inst1(POP, RAX), - self.deref_rax(node), - Assembly::inst1(PUSH, RAX), - ] - .into() - }, + format!(".Lgoto.{}:", node.value.unwrap()).into(), + self.gen_statements( + std::slice::from_ref(node.lhs.as_deref().unwrap()), + offset, + breakable_branch_num, + ), ] .into(); } - NodeType::Assign => { + NodeType::FloatAbs => { + let shift = 65 - node.resolve_type().unwrap().float_bits().unwrap() as usize; return vec![ - self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - self.gen_node(node.rhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RDI), + self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), Assembly::inst1(POP, RAX), - self.operation2rdi(node.lhs.as_ref().unwrap().resolve_type(), MOV, RAX), - self.deref_rax(node.lhs.as_ref().unwrap()), + Assembly::inst2(SHL, RAX, shift), + Assembly::inst2(SHR, RAX, shift), Assembly::inst1(PUSH, RAX), ] .into(); } - NodeType::BitLeft | NodeType::BitRight => { - return vec![ - self.gen_node(node.rhs.as_ref().unwrap(), offset, breakable_branch_num), + NodeType::BitScan { bits, leading } => { + let (input, count) = if bits == 32 { (EAX, ECX) } else { (RAX, RCX) }; + let mut lines = vec![ self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), Assembly::inst1(POP, RAX), - Assembly::inst1(POP, RCX), + Assembly::inst2(if leading { BSR } else { BSF }, count, input), Assembly::inst2( - match node.nt { - NodeType::BitLeft => SHL, - NodeType::BitRight => { - if node.resolve_type().unwrap().integer_info().unwrap().1 { - SAR - } else { - SHR - } - } - _ => { - unreachable!() - } + MOV, + R11, + if leading { + usize::MAX + } else { + usize::from(bits) }, - RAX, - CL, ), - Self::normalize_integer(&node.resolve_type().unwrap(), RAX), - Assembly::inst1(PUSH, RAX), - ] - .into(); + // BSF/BSR leave the destination undefined for zero; select a fallback first. + Assembly::inst2(CMOVE, RCX, R11), + ]; + if leading { + lines.extend([ + Assembly::inst2(MOV, RAX, usize::from(bits - 1)), + Assembly::inst2(SUB, RAX, RCX), + ]); + } else { + lines.push(Assembly::inst2(MOV, RAX, RCX)); + } + lines.push(Assembly::inst1(PUSH, RAX)); + return lines.into(); } - NodeType::BitNot => { - return vec![ + NodeType::VaStart | NodeType::VaEnd => { + let mut lines = vec![ self.gen_node(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RAX), - Assembly::inst1(NOT, RAX), - Self::normalize_integer(&node.resolve_type().unwrap(), RAX), - Assembly::inst1(PUSH, RAX), - ] - .into(); + Assembly::inst1(POP, R11), + ]; + if node.nt == NodeType::VaStart { + let (gp, fp, stack) = crate::generator::abi::variadic_state( + node, + crate::generator::Arch::X86_64, + self.target_os, + ); + lines.extend([ + Assembly::inst2(MOV, EAX, gp * 8), + Assembly::inst2(MOV, PtrOffset(R11, 0, 4), EAX), + Assembly::inst2(MOV, EAX, 48 + fp * 16), + Assembly::inst2(MOV, PtrOffset(R11, 4, 4), EAX), + Assembly::inst2(LEA, RAX, PtrOffset(RBP, 16 + stack, 8)), + Assembly::inst2(MOV, PtrOffset(R11, 8, 8), RAX), + Assembly::inst2(MOV, RAX, RBP), + Assembly::inst2(SUB, RAX, offset), + Assembly::inst2(MOV, PtrOffset(R11, 16, 8), RAX), + ]); + } + lines.push(Assembly::inst1(PUSH, 0)); + return lines.into(); } - NodeType::LogicalAnd => { - let branch_num = node.token.as_ref().unwrap().pos; - return vec![ - self.gen_node(node.lhs.as_ref().unwrap(), offset, branch_num), - Assembly::inst1(POP, RAX), - Assembly::inst2(CMP, RAX, 0), - Assembly::inst1(JE, EndFlag(branch_num)), - self.gen_node(node.rhs.as_ref().unwrap(), offset, branch_num), - Assembly::inst1(POP, RAX), - format!("{}:", EndFlag(branch_num)).into(), - Assembly::inst2(CMP, RAX, 0), - Assembly::inst1(SETNE, AL), - Assembly::inst2(MOVZX, RAX, AL), - Assembly::inst1(PUSH, RAX), - ] - .into(); + NodeType::Prefetch(_, locality) => { + return self.gen_prefetch(node, locality, offset, breakable_branch_num); } - NodeType::LogicalOr => { - let branch_num = node.token.as_ref().unwrap().pos; + NodeType::Atomic(operation) => { + return self.gen_atomic(node, operation, offset, breakable_branch_num); + } + NodeType::InlineAsm(_, _) => { + return self.gen_inline_assembly(node, offset, breakable_branch_num); + } + NodeType::ZeroFill { bytes } => { return vec![ - self.gen_node(node.lhs.as_ref().unwrap(), offset, branch_num), - Assembly::inst1(POP, RAX), - Assembly::inst2(CMP, RAX, 0), - Assembly::inst1(JNE, EndFlag(branch_num)), - self.gen_node(node.rhs.as_ref().unwrap(), offset, branch_num), - Assembly::inst1(POP, RAX), - format!("{}:", EndFlag(branch_num)).into(), - Assembly::inst2(CMP, RAX, 0), - Assembly::inst1(SETNE, AL), - Assembly::inst2(MOVZX, RAX, AL), - Assembly::inst1(PUSH, RAX), + self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RDI), + Assembly::inst2(MOV, RCX, bytes), + Assembly::inst2(XOR, EAX, EAX), + Assembly::inst0(REPSTOSB), + Assembly::inst1(PUSH, 0), ] .into(); } - NodeType::SuffixIncr | NodeType::SuffixDecr => { - let op = if let NodeType::SuffixIncr = node.nt { - ADD - } else { - SUB - }; - if node.resolve_type() == Some(Type::Bool) { - return vec![ - self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RAX), - Assembly::inst2(MOV, RDX, RAX), - self.deref_rax(node.lhs.as_ref().unwrap()), - Assembly::inst2(MOV, RDI, RAX), - Assembly::inst2(op, RDI, 1), - Self::normalize_integer(&Type::Bool, RDI), - Assembly::inst2(MOV, Ptr(RDX, 1), DIL), - Assembly::inst1(PUSH, RAX), - ] - .into(); - } + NodeType::CopyBytes { bytes } => { return vec![ self.gen_addr(node.lhs.as_ref().unwrap(), offset, breakable_branch_num), - Assembly::inst1(POP, RAX), - Assembly::inst2(MOV, RDI, 1), - if let Some(t) = node.lhs.as_ref().unwrap().dest_type() { - Assembly::inst2(IMUL, RDI, t.size_of()) - } else { - vec![].into() - }, - Assembly::inst2(MOV, RDX, RAX), - self.deref_rax(node.lhs.as_ref().unwrap()), - self.operation2rdi(node.lhs.as_ref().unwrap().resolve_type(), op, RDX), - Assembly::inst1(PUSH, RAX), + self.gen_addr(node.rhs.as_ref().unwrap(), offset, breakable_branch_num), + Assembly::inst1(POP, RSI), + Assembly::inst1(POP, RDI), + Assembly::inst1(PUSH, RDI), + Assembly::inst2(MOV, RCX, bytes), + Assembly::inst0(REPMOVSB), ] .into(); } @@ -1233,6 +1232,9 @@ impl<'a> AsmGenerator<'a> { Assembly::inst2(MOV, RAX, RDX), ] .into(), + NodeType::BitAnd => Assembly::inst2(AND, RAX, RDI), + NodeType::BitXor => Assembly::inst2(XOR, RAX, RDI), + NodeType::BitOr => Assembly::inst2(OR, RAX, RDI), NodeType::Eq | NodeType::Ne | NodeType::Lt | NodeType::Le => vec![ Assembly::inst2(CMP, RAX, RDI), Assembly::inst1( @@ -1260,9 +1262,6 @@ impl<'a> AsmGenerator<'a> { Assembly::inst2(MOVZX, RAX, AL), ] .into(), - NodeType::BitAnd => Assembly::inst2(AND, RAX, RDI), - NodeType::BitXor => Assembly::inst2(XOR, RAX, RDI), - NodeType::BitOr => Assembly::inst2(OR, RAX, RDI), _ => { self.error_logger .print_error_position(node.token.as_ref().unwrap().pos, "unexpected node"); @@ -1281,19 +1280,19 @@ impl<'a> AsmGenerator<'a> { self.gen_node(node, offset, branch_num), if matches!( node.nt, - NodeType::Block - | NodeType::DefVar + NodeType::DefVar + | NodeType::Block + | NodeType::Return | NodeType::If | NodeType::While | NodeType::DoWhile | NodeType::For | NodeType::Switch | NodeType::Case - | NodeType::Label - | NodeType::Goto | NodeType::Break | NodeType::Continue - | NodeType::Return + | NodeType::Goto + | NodeType::Label ) { vec![].into() } else { @@ -1308,22 +1307,12 @@ impl<'a> AsmGenerator<'a> { fn gen_addr(&self, node: &Node, offset: usize, branch_num: usize) -> Assembly { match node.nt { - NodeType::CopyBytes { .. } => self.gen_node(node, offset, branch_num), - NodeType::CallFunc => self.gen_node(node, offset, branch_num), - NodeType::Conditional => self.gen_node(node, offset, branch_num), - - NodeType::Sequence => { - let mut lines = vec![]; - for child in &node.children[..node.children.len() - 1] { - lines.push(self.gen_node(child, offset, branch_num)); - if !matches!(child.nt, NodeType::Block | NodeType::DefVar) { - lines.push(Assembly::inst1(POP, RAX)); - } - } - lines.push(self.gen_addr(node.children.last().unwrap(), offset, branch_num)); - lines.into() - } - + NodeType::LocalVar => vec![ + Assembly::inst2(MOV, RAX, RBP), + Assembly::inst2(SUB, RAX, node.offset.unwrap()), + Assembly::inst1(PUSH, RAX), + ] + .into(), NodeType::GlobalVar => vec![ if !node.dest.is_empty() { Assembly::inst2(LEA, RAX, PtrAdd(RIP, node.dest.clone().replace('@', "L_"))) @@ -1342,13 +1331,21 @@ impl<'a> AsmGenerator<'a> { Assembly::inst1(PUSH, RAX), ] .into(), - NodeType::LocalVar => vec![ - Assembly::inst2(MOV, RAX, RBP), - Assembly::inst2(SUB, RAX, node.offset.unwrap()), - Assembly::inst1(PUSH, RAX), - ] - .into(), NodeType::Deref => self.gen_node(node.lhs.as_ref().unwrap(), offset, branch_num), + NodeType::Conditional => self.gen_node(node, offset, branch_num), + NodeType::CallFunc => self.gen_node(node, offset, branch_num), + NodeType::Sequence => { + let mut lines = vec![]; + for child in &node.children[..node.children.len() - 1] { + lines.push(self.gen_node(child, offset, branch_num)); + if !matches!(child.nt, NodeType::DefVar | NodeType::Block) { + lines.push(Assembly::inst1(POP, RAX)); + } + } + lines.push(self.gen_addr(node.children.last().unwrap(), offset, branch_num)); + lines.into() + } + NodeType::CopyBytes { .. } => self.gen_node(node, offset, branch_num), _ => { unreachable!(); } diff --git a/src/generator/x86_64/generator/atomic.rs b/src/generator/x86_64/generator/atomic.rs index bb9e3e6..58f7d36 100644 --- a/src/generator/x86_64/generator/atomic.rs +++ b/src/generator/x86_64/generator/atomic.rs @@ -25,10 +25,10 @@ impl AsmGenerator<'_> { Assembly::inst2(MOV, RCX, RAX), ]; lines.push(match operation { - AtomicRmwOperation::Exchange => Assembly::inst2(MOV, RCX, RSI), AtomicRmwOperation::Nand => { vec![Assembly::inst2(AND, RCX, RSI), Assembly::inst1(NOT, RCX)].into() } + AtomicRmwOperation::Exchange => Assembly::inst2(MOV, RCX, RSI), _ => Assembly::inst2( match operation { AtomicRmwOperation::Add => ADD, diff --git a/src/generator/x86_64/generator/globals.rs b/src/generator/x86_64/generator/globals.rs index e84f444..90c7bb0 100644 --- a/src/generator/x86_64/generator/globals.rs +++ b/src/generator/x86_64/generator/globals.rs @@ -45,8 +45,8 @@ impl AsmGenerator<'_> { pub(super) fn weak_reference(&self, name: &str) -> Assembly { let directive = match self.target_os { - Os::MacOS => ".weak_reference", Os::Linux => ".weak", + Os::MacOS => ".weak_reference", }; format!("{directive} {}", self.with_prefix(name)).into() } @@ -62,8 +62,8 @@ impl AsmGenerator<'_> { if gv.common && !gv.internal && !gv.thread_local && gv.data.is_none() { let alignment = gv.ty.align_of().max(gv.alignment).max(1); let alignment = match self.target_os { - Os::MacOS => alignment.trailing_zeros() as usize, Os::Linux => alignment, + Os::MacOS => alignment.trailing_zeros() as usize, }; return format!( ".comm {},{},{}", @@ -106,7 +106,94 @@ impl AsmGenerator<'_> { data }; match ty { - Type::Aligned(child, _) => self.gen_initializer_element(child, data), + Type::Bool | Type::Integer { .. } | Type::LongLong { .. } => format!( + " {} {}", + if ty.size_of() == 1 { + ".byte".to_string() + } else { + format!(".{}byte", ty.size_of()) + }, + if let Some(GlobalVariableData::Address { symbol, offset }) = data { + format!("{}{offset:+}", self.address_symbol(symbol)) + } else if let Some(GlobalVariableData::Elem(value)) = data { + ty.convert_constant(value.parse().unwrap()).to_string() + } else { + "0".into() + } + ) + .into(), + Type::I8 | Type::SignedChar => format!( + " .byte {}", + if let Some(GlobalVariableData::Elem(s)) = data { + s + } else { + "0" + } + ) + .into(), + Type::I32 => format!( + " .4byte {}", + if let Some(GlobalVariableData::Elem(s)) = data { + s + } else { + "0" + } + ) + .into(), + Type::Integer128 { .. } => " .zero 16".into(), + Type::Enum(value) => self.gen_initializer_element(&value.underlying.borrow(), data), + Type::Float16 => " .zero 2".into(), + Type::LongDouble(bits) if *bits != 64 => { + let value = if let Some(GlobalVariableData::Elem(value)) = data { + value.parse::().unwrap() + } else { + 0 + }; + format!( + " .8byte {}\n .8byte {}", + value as u64, + (value >> 64) as u64 + ) + .into() + } + Type::Float(_) | Type::LongDouble(_) => format!( + " .{}byte {}", + ty.size_of(), + if let Some(GlobalVariableData::Elem(value)) = data { + value.as_str() + } else { + "0" + } + ) + .into(), + Type::Arr(children_ty, size) => { + let size = size.unwrap_or(0); + if let Some(GlobalVariableData::Arr(v)) = data { + vec![ + v.iter() + .enumerate() + .filter(|(i, _)| *i < size) + .map(|(_, d)| { + self.gen_initializer_element(children_ty.as_ref(), Some(d)) + }) + .collect::>() + .into(), + format!( + " .zero {}", + children_ty.size_of() + * v.iter() + .enumerate() + .filter(|(i, _)| *i < size) + .fold(size, |_, _| { size.saturating_sub(v.len()) }) + ) + .into(), + ] + .into() + } else { + format!(" .zero {}", children_ty.size_of() * size).into() + } + } + Type::Vector(..) => format!(" .zero {}", ty.size_of()).into(), Type::Struct(struct_type) if struct_type.has_bit_fields() || struct_type.definition().borrow().is_union => { @@ -159,94 +246,7 @@ impl AsmGenerator<'_> { } lines.into() } - Type::Arr(children_ty, size) => { - let size = size.unwrap_or(0); - if let Some(GlobalVariableData::Arr(v)) = data { - vec![ - v.iter() - .enumerate() - .filter(|(i, _)| *i < size) - .map(|(_, d)| { - self.gen_initializer_element(children_ty.as_ref(), Some(d)) - }) - .collect::>() - .into(), - format!( - " .zero {}", - children_ty.size_of() - * v.iter() - .enumerate() - .filter(|(i, _)| *i < size) - .fold(size, |_, _| { size.saturating_sub(v.len()) }) - ) - .into(), - ] - .into() - } else { - format!(" .zero {}", children_ty.size_of() * size).into() - } - } - Type::Enum(value) => self.gen_initializer_element(&value.underlying.borrow(), data), - Type::I8 | Type::SignedChar => format!( - " .byte {}", - if let Some(GlobalVariableData::Elem(s)) = data { - s - } else { - "0" - } - ) - .into(), - Type::Integer128 { .. } => " .zero 16".into(), - Type::Float16 => " .zero 2".into(), - Type::Vector(..) => format!(" .zero {}", ty.size_of()).into(), - Type::LongDouble(bits) if *bits != 64 => { - let value = if let Some(GlobalVariableData::Elem(value)) = data { - value.parse::().unwrap() - } else { - 0 - }; - format!( - " .8byte {}\n .8byte {}", - value as u64, - (value >> 64) as u64 - ) - .into() - } - Type::Float(_) | Type::LongDouble(_) => format!( - " .{}byte {}", - ty.size_of(), - if let Some(GlobalVariableData::Elem(value)) = data { - value.as_str() - } else { - "0" - } - ) - .into(), - Type::Integer { .. } | Type::LongLong { .. } | Type::Bool => format!( - " {} {}", - if ty.size_of() == 1 { - ".byte".to_string() - } else { - format!(".{}byte", ty.size_of()) - }, - if let Some(GlobalVariableData::Address { symbol, offset }) = data { - format!("{}{offset:+}", self.address_symbol(symbol)) - } else if let Some(GlobalVariableData::Elem(value)) = data { - ty.convert_constant(value.parse().unwrap()).to_string() - } else { - "0".into() - } - ) - .into(), - Type::I32 => format!( - " .4byte {}", - if let Some(GlobalVariableData::Elem(s)) = data { - s - } else { - "0" - } - ) - .into(), + Type::Aligned(child, _) => self.gen_initializer_element(child, data), _ => format!( " .8byte {}", if let Some(GlobalVariableData::Address { symbol, offset }) = data { diff --git a/src/generator/x86_64/instruction.rs b/src/generator/x86_64/instruction.rs index 672996a..b141558 100644 --- a/src/generator/x86_64/instruction.rs +++ b/src/generator/x86_64/instruction.rs @@ -87,14 +87,46 @@ impl InstOperator { // return "movzb"; // } match self { - PREFETCHNTA => "prefetchnta", - PREFETCHT0 => "prefetcht0", - PREFETCHT1 => "prefetcht1", - PREFETCHT2 => "prefetcht2", - InlineAsm => "", - CMPXCHG => "lock cmpxchg", - MFENCE => "mfence", - XCHG => "xchg", + MOV => "mov", + MOVSX => "movsx", + MOVSXD => "movsxd", + MOVZX => "movzx", + LEA => "lea", + PUSH => "push", + POP => "pop", + REPSTOSB => "rep stosb", + REPMOVSB => "rep movsb", + ADD => "add", + SUB => "sub", + IMUL => "imul", + IDIV => "idiv", + DIV => "div", + CQO => "cqo", + AND => "and", + OR => "or", + XOR => "xor", + NOT => "not", + SHL => "shl", + SHR => "shr", + SAR => "sar", + BSF => "bsf", + BSR => "bsr", + CMP => "cmp", + SETE => "sete", + SETNE => "setne", + SETL => "setl", + SETLE => "setle", + SETB => "setb", + SETBE => "setbe", + SETNP => "setnp", + SETP => "setp", + CMOVE => "cmove", + JMP => "jmp", + JE => "je", + JNE => "jne", + JGE => "jge", + CALL => "call", + RET => "ret", MOVD => "movd", MOVQ => "movq", ADDSS => "addss", @@ -109,50 +141,18 @@ impl InstOperator { UCOMISD => "ucomisd", CVTSS2SD => "cvtss2sd", CVTSD2SS => "cvtsd2ss", - CVTSI2SD => "cvtsi2sd", CVTSI2SS => "cvtsi2ss", - CVTTSD2SI => "cvttsd2si", + CVTSI2SD => "cvtsi2sd", CVTTSS2SI => "cvttss2si", - SETNP => "setnp", - SETP => "setp", - PUSH => "push", - POP => "pop", - REPSTOSB => "rep stosb", - REPMOVSB => "rep movsb", - BSF => "bsf", - BSR => "bsr", - CMOVE => "cmove", - MOV => "mov", - ADD => "add", - SUB => "sub", - RET => "ret", - CALL => "call", - CQO => "cqo", - JMP => "jmp", - JE => "je", - JNE => "jne", - JGE => "jge", - IMUL => "imul", - IDIV => "idiv", - DIV => "div", - SHR => "shr", - SETB => "setb", - SETBE => "setbe", - SHL => "shl", - SAR => "sar", - NOT => "not", - SETE => "sete", - SETNE => "setne", - SETL => "setl", - SETLE => "setle", - LEA => "lea", - MOVSX => "movsx", - MOVSXD => "movsxd", - MOVZX => "movzx", - CMP => "cmp", - AND => "and", - OR => "or", - XOR => "xor", + CVTTSD2SI => "cvttsd2si", + CMPXCHG => "lock cmpxchg", + XCHG => "xchg", + MFENCE => "mfence", + PREFETCHNTA => "prefetchnta", + PREFETCHT0 => "prefetcht0", + PREFETCHT1 => "prefetcht1", + PREFETCHT2 => "prefetcht2", + InlineAsm => "", } } } @@ -196,31 +196,30 @@ impl Display for Register { return write!(f, "xmm{index}"); } f.write_str(match self { - Self::Xmm(_) => unreachable!(), - R11 => "r11", - DL => "dl", - RAX => "rax", EAX => "eax", + AX => "ax", + AL => "al", RBX => "rbx", RCX => "rcx", ECX => "ecx", CX => "cx", + CL => "cl", RDX => "rdx", + DL => "dl", RSI => "rsi", RDI => "rdi", - RBP => "rbp", - RSP => "rsp", EDI => "edi", DI => "di", DIL => "dil", - AX => "ax", - AL => "al", - CL => "cl", - RIP => "rip", R8 => "r8", R9 => "r9", R10 => "r10", + R11 => "r11", + RSP => "rsp", + RBP => "rbp", + RIP => "rip", + Self::Xmm(_) => unreachable!(), })?; Ok(()) } @@ -245,11 +244,17 @@ pub enum InstOperand { impl Display for InstOperand { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { f.write_str(&match self { - InstOperand::InlineAsmTemplate(template) => template.clone(), InstOperand::Reg(r) => format!("{}", r), InstOperand::Num(i) => format!("{}", i), - InstOperand::Label(l) => l.clone(), InstOperand::Str(s) => String::from(*s), + InstOperand::Label(l) => l.clone(), + InstOperand::Ptr(r, i) => match i { + 1 => format!("byte ptr[{}]", r), + 2 => format!("word ptr[{}]", r), + 4 => format!("dword ptr[{}]", r), + 8 => format!("qword ptr[{}]", r), + _ => unreachable!(), + }, InstOperand::PtrOffset(r, offset, size) => format!( "{} ptr[{} + {}]", match size { @@ -262,17 +267,11 @@ impl Display for InstOperand { r, offset ), - InstOperand::Ptr(r, i) => match i { - 1 => format!("byte ptr[{}]", r), - 2 => format!("word ptr[{}]", r), - 4 => format!("dword ptr[{}]", r), - 8 => format!("qword ptr[{}]", r), - _ => unreachable!(), - }, - InstOperand::ElseFlag(i) => format!(".Lelse{}", i), + InstOperand::PtrAdd(s, r) => format!("[{} + {}]", s, r), InstOperand::BeginFlag(i) => format!(".Lbegin{}", i), + InstOperand::ElseFlag(i) => format!(".Lelse{}", i), InstOperand::EndFlag(i) => format!(".Lend{}", i), - InstOperand::PtrAdd(s, r) => format!("[{} + {}]", s, r), + InstOperand::InlineAsmTemplate(template) => template.clone(), }) } } diff --git a/src/global.rs b/src/global.rs index 06ff6e2..f8c4033 100644 --- a/src/global.rs +++ b/src/global.rs @@ -36,6 +36,22 @@ pub fn initializer_bytes( addresses: &mut HashMap, ) { match ty.unqualified() { + Type::Arr(child, size) => { + let elements = if let Some(GlobalVariableData::Arr(v)) = data { + Some(v) + } else { + None + }; + for index in 0..size.unwrap_or(0) { + initializer_bytes( + child, + elements.and_then(|v| v.get(index)), + offset + index * child.size_of(), + bytes, + addresses, + ); + } + } Type::Struct(st) => { let elements = if let Some(GlobalVariableData::Arr(v)) = data { Some(v) @@ -79,22 +95,6 @@ pub fn initializer_bytes( } } } - Type::Arr(child, size) => { - let elements = if let Some(GlobalVariableData::Arr(v)) = data { - Some(v) - } else { - None - }; - for index in 0..size.unwrap_or(0) { - initializer_bytes( - child, - elements.and_then(|v| v.get(index)), - offset + index * child.size_of(), - bytes, - addresses, - ); - } - } _ => { let mut data = data; while let Some(GlobalVariableData::Arr(v)) = data { diff --git a/src/node.rs b/src/node.rs index b31268d..52488ee 100644 --- a/src/node.rs +++ b/src/node.rs @@ -169,19 +169,19 @@ impl Node { fn has_effects(node: &Self, value: bool) -> bool { if matches!( node.nt, - NodeType::InlineAsm(_, _) - | NodeType::Prefetch(..) - | NodeType::Atomic(_) - | NodeType::Assign - | NodeType::ZeroFill { .. } - | NodeType::CopyBytes { .. } + NodeType::Assign + | NodeType::SuffixIncr + | NodeType::SuffixDecr | NodeType::CallFunc | NodeType::AddOverflow | NodeType::SubOverflow - | NodeType::SuffixIncr - | NodeType::SuffixDecr | NodeType::VaStart | NodeType::VaEnd + | NodeType::Prefetch(..) + | NodeType::Atomic(_) + | NodeType::InlineAsm(_, _) + | NodeType::ZeroFill { .. } + | NodeType::CopyBytes { .. } ) || (value && (node.volatile_access || node.object_type().is_some_and(|ty| { @@ -218,8 +218,8 @@ impl Node { } } let mut node = Self { - token, nt, + token, lhs: Some(Box::new(lhs)), rhs: Some(Box::new(rhs)), ..Self::default() @@ -229,8 +229,8 @@ impl Node { } pub fn new_with_op_and_lhs(token: Option, nt: NodeType, lhs: Node) -> Self { let mut node = Self { - token, nt, + token, lhs: Some(Box::new(lhs)), ..Self::default() }; @@ -249,12 +249,12 @@ impl Node { | NodeType::Mul | NodeType::Div | NodeType::Mod + | NodeType::BitLeft + | NodeType::BitRight | NodeType::BitAnd | NodeType::BitXor | NodeType::BitOr | NodeType::BitNot - | NodeType::BitLeft - | NodeType::BitRight ) { self.cty = self .infer_arithmetic_type_view() @@ -288,7 +288,7 @@ impl Node { &lhs, self.rhs.as_ref()?.resolve_type_view()?.as_ref(), ))), - NodeType::BitNot | NodeType::BitLeft | NodeType::BitRight => { + NodeType::BitLeft | NodeType::BitRight | NodeType::BitNot => { Some(Cow::Owned(lhs.promoted())) } _ => None, @@ -297,16 +297,16 @@ impl Node { pub fn new_with_num(token: Option, value: usize) -> Self { Self { - token, nt: NodeType::Num, + token, value: Some(value), ..Self::default() } } pub fn new_if_node(token: Option, cond: Node, then: Node, els: Option) -> Self { Self { - token, nt: NodeType::If, + token, cond: Some(Box::new(cond)), then: Some(Box::new(then)), els: els.map(Box::new), @@ -315,8 +315,8 @@ impl Node { } pub fn new_while_node(token: Option, cond: Node, then: Node) -> Self { Self { - token, nt: NodeType::While, + token, cond: Some(Box::new(cond)), then: Some(Box::new(then)), ..Self::default() @@ -330,12 +330,12 @@ impl Node { then: Node, ) -> Self { Self { - token, nt: NodeType::For, - ini: ini.map(Box::new), + token, cond: cond.map(Box::new), - upd: upd.map(Box::new), then: Some(Box::new(then)), + ini: ini.map(Box::new), + upd: upd.map(Box::new), ..Self::default() } } @@ -413,6 +413,50 @@ impl Node { } pub(crate) fn resolve_qualified_type_view(&self) -> Option> { match self.nt { + NodeType::Num => Some( + self.cty + .as_ref() + .map_or(Cow::Owned(Type::I32), Cow::Borrowed), + ), + NodeType::LocalVar + | NodeType::GlobalVar + | NodeType::CallFunc + | NodeType::AddOverflow + | NodeType::SubOverflow + | NodeType::BitScan { .. } + | NodeType::VaStart + | NodeType::VaEnd + | NodeType::StackAlloc + | NodeType::Prefetch(..) + | NodeType::Atomic(_) + | NodeType::ZeroFill { .. } + | NodeType::CopyBytes { .. } => self.cty.as_ref().map(Cow::Borrowed), + NodeType::Addr => { + if let Some(ty) = self.lhs.as_ref().unwrap().object_type_view() { + Some(Cow::Owned(Type::Ptr(Rc::new(ty.into_owned())))) + } else { + self.cty.as_ref().map(Cow::Borrowed) + } + } + NodeType::Deref => self.lhs.as_ref().unwrap().dest_type_view(), + NodeType::Cast | NodeType::FloatAbs | NodeType::InlineAsm(_, _) => { + self.cty.as_ref().map(Cow::Borrowed) + } + NodeType::Add + | NodeType::Sub + | NodeType::Mul + | NodeType::Div + | NodeType::Mod + | NodeType::BitLeft + | NodeType::BitRight + | NodeType::BitAnd + | NodeType::BitXor + | NodeType::BitOr + | NodeType::BitNot => self + .cty + .as_ref() + .map(Cow::Borrowed) + .or_else(|| self.infer_arithmetic_type_view()), NodeType::Eq | NodeType::Ne | NodeType::Lt @@ -450,51 +494,7 @@ impl Node { then } } - NodeType::Cast | NodeType::FloatAbs | NodeType::InlineAsm(_, _) => { - self.cty.as_ref().map(Cow::Borrowed) - } - NodeType::Num => Some( - self.cty - .as_ref() - .map_or(Cow::Owned(Type::I32), Cow::Borrowed), - ), NodeType::Sequence => self.children.last().and_then(Node::resolve_type_view), - NodeType::Add - | NodeType::Sub - | NodeType::Mul - | NodeType::Div - | NodeType::Mod - | NodeType::BitAnd - | NodeType::BitXor - | NodeType::BitOr - | NodeType::BitNot - | NodeType::BitLeft - | NodeType::BitRight => self - .cty - .as_ref() - .map(Cow::Borrowed) - .or_else(|| self.infer_arithmetic_type_view()), - NodeType::Prefetch(..) - | NodeType::StackAlloc - | NodeType::BitScan { .. } - | NodeType::ZeroFill { .. } - | NodeType::CopyBytes { .. } - | NodeType::Atomic(_) - | NodeType::LocalVar - | NodeType::CallFunc - | NodeType::AddOverflow - | NodeType::SubOverflow - | NodeType::GlobalVar - | NodeType::VaStart - | NodeType::VaEnd => self.cty.as_ref().map(Cow::Borrowed), - NodeType::Addr => { - if let Some(ty) = self.lhs.as_ref().unwrap().object_type_view() { - Some(Cow::Owned(Type::Ptr(Rc::new(ty.into_owned())))) - } else { - self.cty.as_ref().map(Cow::Borrowed) - } - } - NodeType::Deref => self.lhs.as_ref().unwrap().dest_type_view(), _ => { if let Some(node) = self.lhs.as_ref() { if let Some(ty) = node.resolve_type_view() { @@ -529,6 +529,9 @@ impl Node { } pub fn to_debug_string(&self, indent: usize) -> String { match self.nt { + NodeType::Num => { + format!("Num: {}", self.value.unwrap()) + } NodeType::LocalVar => { format!( "LocalVar: {{ type: {:?}, offset: {} }}", @@ -539,9 +542,6 @@ impl Node { NodeType::GlobalVar => { format!("GlobalVar: {{ name: {}{} }}", &self.global_name, &self.dest) } - NodeType::Num => { - format!("Num: {}", self.value.unwrap()) - } _ => [ vec![format!("{:?}:", self.nt)], self.ini diff --git a/src/preprocessor/native.rs b/src/preprocessor/native.rs index fdfa110..b2ea3a5 100644 --- a/src/preprocessor/native.rs +++ b/src/preprocessor/native.rs @@ -288,27 +288,12 @@ pub(super) struct Preprocessor { impl Preprocessor { pub(super) fn new(includes: Vec, quotes: Vec) -> Self { Self { - macros: HashMap::new(), includes, - frameworks: vec![], quotes, - include_cache: HashMap::new(), - search_origins: HashMap::new(), - headers: HashMap::new(), - once: HashSet::new(), - counter: 0, - depth: 0, - output: String::new(), - base_file: String::new(), - dependencies: vec![], - seen_dependencies: HashSet::new(), + frameworks: vec![], warnings_as_errors: false, dependency_only: false, - condition_expansion: false, - stateful: [SmolStr::new("__COUNTER__"), SmolStr::new("_Pragma")] - .into_iter() - .collect(), - dependents: HashMap::new(), + macros: HashMap::new(), builtin_names: [ "__LINE__", "__FILE__", @@ -321,7 +306,22 @@ impl Preprocessor { .into_iter() .map(SmolStr::new) .collect(), + stateful: [SmolStr::new("__COUNTER__"), SmolStr::new("_Pragma")] + .into_iter() + .collect(), + dependents: HashMap::new(), include_context: PathBuf::new(), + search_origins: HashMap::new(), + include_cache: HashMap::new(), + headers: HashMap::new(), + once: HashSet::new(), + depth: 0, + base_file: String::new(), + counter: 0, + condition_expansion: false, + output: String::new(), + dependencies: vec![], + seen_dependencies: HashSet::new(), } } diff --git a/src/token.rs b/src/token.rs index e0d6f09..f3ae118 100644 --- a/src/token.rs +++ b/src/token.rs @@ -133,17 +133,17 @@ impl Token { impl Token { pub fn print(&self) { match self.tt { - TokenType::Float => eprintln!("float: {}", self.s_value), - TokenType::Version => eprintln!("version: {}", self.s_value), - TokenType::Num => { - eprintln!("num: {}", self.i_value) - } TokenType::Reserved => { eprintln!("rsv: {}", &self.s_value) } TokenType::Ident => { eprintln!("idt: {}", &self.s_value) } + TokenType::Num => { + eprintln!("num: {}", self.i_value) + } + TokenType::Float => eprintln!("float: {}", self.s_value), + TokenType::Version => eprintln!("version: {}", self.s_value), TokenType::Str => { eprintln!("str: {}", &self.s_value) } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index ddd5ffd..ac8daf6 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -185,6 +185,7 @@ fn encode_string_literal(value: &str, encoding: StringEncoding) -> Vec { for character in text.chars() { let scalar = character as u32; match encoding { + StringEncoding::Narrow | StringEncoding::Utf8 => unreachable!(), StringEncoding::Utf16 => { let value = scalar as u16; if scalar > 0xffff { @@ -201,7 +202,6 @@ fn encode_string_literal(value: &str, encoding: StringEncoding) -> Vec { } StringEncoding::Utf32 => bytes.extend_from_slice(&scalar.to_ne_bytes()), StringEncoding::Wide => bytes.extend_from_slice(&scalar.to_ne_bytes()), - StringEncoding::Narrow | StringEncoding::Utf8 => unreachable!(), } } bytes @@ -488,10 +488,8 @@ impl Tokenizer { .map_err(|_| error::SyntaxError::new(start, "invalid floating literal"))? }; Ok(Some(( - Token { - tt: TokenType::Float, - pos: start, - extended_float: if extended && cfg!(target_arch = "x86_64") { + { + let extended_float = if extended && cfg!(target_arch = "x86_64") { use rustc_apfloat::Float; let value = code[start..numeric_end] .parse::() @@ -501,21 +499,26 @@ impl Tokenizer { Some(Box::new(value.to_bits())) } else { None - }, - i_value: if single { - (value as f32).to_bits() as usize - } else { - value.to_bits() as usize - }, - s_value: if single { - "f" - } else if extended { - "l" - } else { - "d" + }; + Token { + tt: TokenType::Float, + pos: start, + s_value: if single { + "f" + } else if extended { + "l" + } else { + "d" + } + .into(), + i_value: if single { + (value as f32).to_bits() as usize + } else { + value.to_bits() as usize + }, + extended_float, + ..Token::default() } - .into(), - ..Token::default() }, pos, )))