diff --git a/src/ast.rs b/src/ast.rs index 766fc22..44e5989 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -29,33 +29,44 @@ mod varargs; mod vla; pub struct AstBuilderImpl<'a> { + // Input and diagnostics. error_logger: &'a dyn error::ErrorLogger, tokens: &'a Vec, cur: usize, - offset_size: usize, - loop_stack: Vec, - labels: HashMap, - defined_labels: HashSet, - switch_stack: Vec>, - switch_types: Vec, - return_type: Option, + + // Scopes, types and declaration context. + scope_stack: Vec>, + struct_definitions: Vec>>, + next_enum_id: usize, + declaration_attributes_state: Option, + parameter_depth: usize, + + // Current function. current_function: Option, - mutable_static: Option, + return_type: Option, return_pointer: Option, + mutable_static: Option, variadic_parameters: Option>, variadic_last: Option, + + // Local storage and variable-length arrays. + offset_size: usize, + next_deferred_local: usize, pending_bounds: Vec, - parameter_depth: usize, - declaration_attributes_state: Option, dynamic_objects: Vec, vla_active: Vec, - jump_depths: Vec, + + // Control flow and jump lifetimes. + loop_stack: Vec, continue_depths: HashMap, + switch_stack: Vec>, + switch_types: Vec, switch_vla_depths: Vec, - next_enum_id: usize, - next_deferred_local: usize, - struct_definitions: Vec>>, - scope_stack: Vec>, + labels: HashMap, + defined_labels: HashSet, + jump_depths: Vec, + + // Translation-unit output. pub functions: HashMap, pub global_variables: HashMap, pub string_literals: Vec, @@ -71,16 +82,17 @@ pub struct ProgramAst<'a> { pub enum Identifier { TypeDef(Type), - Constant(i64, Type), EnumTag { ty: Type, fixed: bool, defined: bool, }, - StaticObject(Type, String), + Constant(i64, Type), + Local(Type, usize, bool, usize), Dynamic(Type, usize, bool), Static(Type), + StaticObject(Type, String), } #[derive(Clone, Copy, PartialEq)] @@ -165,14 +177,16 @@ impl Declarator { #[derive(Clone, Default)] struct DeclarationAttributes { + typedef_declarator: bool, + word_mode: bool, + vector_size: Option, + transparent_union: bool, + gnu_inline: bool, noinline: bool, - weak_import: bool, + common: bool, - typedef_declarator: bool, - transparent_union: bool, - vector_size: Option, - word_mode: bool, + weak_import: bool, hidden: bool, } diff --git a/src/bin/dynamite-cc/main.rs b/src/bin/dynamite-cc/main.rs index dd71b73..5a4e47e 100644 --- a/src/bin/dynamite-cc/main.rs +++ b/src/bin/dynamite-cc/main.rs @@ -30,20 +30,23 @@ enum LinkArgument { } struct Arguments { + mode: Mode, + preprocess_only: bool, + emit_llvm: bool, + jobs: usize, + inputs: Vec, + output: Option, + + preprocessing: Vec, backend: Vec, linker: Vec, - preprocessing: Vec, forwarded: Vec, - output: Option, - mode: Mode, - jobs: usize, + dependencies: Option, dependency_file: Option, dependency_targets: Vec, phony_dependencies: bool, - preprocess_only: bool, - emit_llvm: bool, } struct TemporaryDirectory(PathBuf); diff --git a/src/ctype.rs b/src/ctype.rs index 1883157..7ab7735 100644 --- a/src/ctype.rs +++ b/src/ctype.rs @@ -33,18 +33,19 @@ pub struct StructType { #[derive(Default)] pub struct StructDefinition { pub members: Vec, + pub complete: bool, pub is_union: bool, /// Function parameters use the first scalar member's calling convention. pub transparent_union: bool, pub has_bit_fields: bool, pub has_flexible_array: bool, + pub size: usize, pub align: usize, /// Member-derived alignment before record-level alignment attributes. pub natural_align: usize, /// Packed storage marker: a pragma limit, or 1 for GNU packed records. pub packing: Option, - pub complete: bool, } #[derive(Clone, Copy, Debug)] @@ -58,12 +59,14 @@ pub struct BitField { #[derive(Clone, Debug)] pub struct StructMember { pub name: String, - pub anonymous: bool, - /// Explicit member alignment before record packing is applied. - pub requested_alignment: usize, pub ty: Type, + pub offset: usize, + /// Explicit member alignment before record packing is applied. + pub requested_alignment: usize, pub bit_field: Option, + + pub anonymous: bool, } impl StructType { @@ -147,38 +150,43 @@ impl fmt::Debug for StructType { /// Recursive type components are shared; lowering uses copy-on-write mutations. #[derive(Clone, PartialEq, Debug)] pub enum Type { + // Scalar types. Void, - Qualified(Rc, Qualifiers), - /// Object alignment supplied by a scalar typedef, independent of type identity. - Aligned(Rc, usize), - Struct(StructType), - Enum(EnumType), + Bool, I8, SignedChar, I32, - LongLong { + Integer { + bits: u8, signed: bool, }, - Integer128 { + LongLong { signed: bool, }, - Integer { - bits: u8, + Integer128 { signed: bool, }, - Bool, + Enum(EnumType), Float16, - /// Vector element type and lane count; value operations are not yet supported. - Vector(Rc, usize), Float(u8), LongDouble(u8), + + // Derived and aggregate types. Ptr(Rc), /// None denotes an omitted bound; Some(0) is a complete GNU zero-length array. Arr(Rc, Option), // The offset names the captured bound; runtime_size computes the language size. Vla(Rc, usize), + /// Vector element type and lane count; value operations are not yet supported. + Vector(Rc, usize), + Struct(StructType), Func(Rc>, Rc), VariadicFunc(Rc>, Rc), + + // Type wrappers. + Qualified(Rc, Qualifiers), + /// Object alignment supplied by a scalar typedef, independent of type identity. + Aligned(Rc, usize), } impl Type { diff --git a/src/func.rs b/src/func.rs index 786e5c0..6ed6a84 100644 --- a/src/func.rs +++ b/src/func.rs @@ -4,20 +4,23 @@ use crate::token::Token; #[derive(Default)] pub struct Func { + pub token: Option, + pub cty: Type, + pub args: Vec, pub body: Option, + pub mutable_static: Option, + + pub offset_size: usize, + pub return_pointer: Option, + pub internal: bool, + pub external_definition: bool, + pub weak_import: bool, + pub hidden: bool, + pub inline_definition: bool, pub gnu_inline: bool, pub noinline: bool, - pub weak_import: bool, - pub hidden: bool, - pub external_definition: bool, - pub mutable_static: Option, - pub return_pointer: Option, - pub cty: Type, - pub offset_size: usize, - pub token: Option, - pub args: Vec, } impl Func { diff --git a/src/generator/aarch64/instruction.rs b/src/generator/aarch64/instruction.rs index 88530bc..bcb62dd 100644 --- a/src/generator/aarch64/instruction.rs +++ b/src/generator/aarch64/instruction.rs @@ -2,68 +2,76 @@ use crate::generator::Os; #[derive(Default)] pub enum InstOperator { - PRFM, - InlineAsm, - FMOV, - FADD, - FSUB, - FMUL, - FDIV, - FCMP, - FCVT, - SCVTF, - UCVTF, - FCVTZS, - FCVTZU, - - CLZ, - RBIT, + // Data movement and memory operations. MOV, - CSET, SXTB, SXTW, - MVN, + LDR, + STR, + LDRU, + STP, + LDP, + ADRP, + LEA, + MOVSX, + MOVSXD, + MOVZX, + + // Integer operations. ADD, SUB, - RET, - BL, - BLR, - SVC, - CQO, - JMP, - JE, - JNE, MUL, + MSUB, SDIV, UDIV, - SHR, - LDRU, + CQO, + AND, + OR, + XOR, + MVN, + NOT, SHL, + SHR, SAR, - NOT, + CLZ, + RBIT, + + // Comparisons and control flow. + CMP, + CSET, SETE, SETNE, SETL, SETLE, - LEA, - MOVSX, - MOVSXD, - MOVZX, - CMP, - AND, - OR, - XOR, + JMP, + JE, + JNE, + BL, + BLR, + RET, + SVC, + + // Floating-point operations and conversions. + FMOV, + FADD, + FSUB, + FMUL, + FDIV, + FCMP, + FCVT, + SCVTF, + UCVTF, + FCVTZS, + FCVTZU, + + // Atomic operations, prefetching and inline assembly. CLREX, LDAXR, STLXR, LDAR, STLR, - LDR, - STR, - ADRP, - STP, - LDP, - MSUB, + PRFM, + InlineAsm, #[default] NOP, } @@ -145,8 +153,7 @@ impl InstOperator { #[derive(Copy, Clone, Debug)] pub enum Register { - Float(usize, u8), - + // General registers and their available subregisters. X0, W0, X1, @@ -171,12 +178,16 @@ pub enum Register { X20, X29, X30, + SP, + + Float(usize, u8), + + // Legacy operand aliases. AL, CL, RIP, R8, R9, - SP, } use Register::*; @@ -224,16 +235,18 @@ impl Display for Register { } pub enum InstOperand { - InlineAsmTemplate(String), Reg(Register), Num(usize), - Label(String), Str(&'static str), + Label(String), + Ptr(Register, usize), PtrAdd(Register, String), - ElseFlag(usize), + BeginFlag(usize), + ElseFlag(usize), EndFlag(usize), + InlineAsmTemplate(String), } impl Display for InstOperand { diff --git a/src/generator/abi.rs b/src/generator/abi.rs index 70beed9..405b358 100644 --- a/src/generator/abi.rs +++ b/src/generator/abi.rs @@ -26,9 +26,10 @@ pub struct Signature { pub arguments: Vec, pub result: Vec, pub indirect_result: bool, - pub stack_size: usize, - pub floating: usize, + pub general: usize, + pub floating: usize, + pub stack_size: usize, } fn floating_members(ty: &Type, offset: usize, members: &mut Vec) -> bool { diff --git a/src/generator/llvm/generator.rs b/src/generator/llvm/generator.rs index d6d1a71..2218c59 100644 --- a/src/generator/llvm/generator.rs +++ b/src/generator/llvm/generator.rs @@ -15,16 +15,18 @@ use crate::{ #[derive(Debug)] pub struct Options<'a> { + return_ty: Type, + return_pointer: Option, + register_number: &'a mut usize, register_map: &'a mut HashMap, register_queue: &'a mut Vec, - label_number: usize, - terminated: bool, - return_ty: Type, - return_pointer: Option, - break_labels: Vec, expression_slots: HashMap, argument_slots: HashMap<(usize, usize), usize>, + + label_number: usize, + break_labels: Vec, + terminated: bool, } impl Options<'_> { diff --git a/src/generator/x86_64/instruction.rs b/src/generator/x86_64/instruction.rs index 4e3dcf3..672996a 100644 --- a/src/generator/x86_64/instruction.rs +++ b/src/generator/x86_64/instruction.rs @@ -1,14 +1,53 @@ use crate::generator::Os; pub enum InstOperator { - PREFETCHNTA, - PREFETCHT0, - PREFETCHT1, - PREFETCHT2, - InlineAsm, - CMPXCHG, - MFENCE, - XCHG, + // Data movement and memory operations. + MOV, + MOVSX, + MOVSXD, + MOVZX, + LEA, + PUSH, + POP, + REPSTOSB, + REPMOVSB, + + // Integer operations. + ADD, + SUB, + IMUL, + IDIV, + DIV, + CQO, + AND, + OR, + XOR, + NOT, + SHL, + SHR, + SAR, + BSF, + BSR, + + // Comparisons and control flow. + CMP, + SETE, + SETNE, + SETL, + SETLE, + SETB, + SETBE, + SETNP, + SETP, + CMOVE, + JMP, + JE, + JNE, + JGE, + CALL, + RET, + + // Floating-point operations and conversions. MOVD, MOVQ, ADDSS, @@ -23,51 +62,20 @@ pub enum InstOperator { UCOMISD, CVTSS2SD, CVTSD2SS, - CVTSI2SD, CVTSI2SS, - CVTTSD2SI, + CVTSI2SD, CVTTSS2SI, - SETNP, - SETP, + CVTTSD2SI, - PUSH, - POP, - REPSTOSB, - REPMOVSB, - BSF, - BSR, - CMOVE, - MOV, - ADD, - SUB, - RET, - CALL, - CQO, - JMP, - JE, - JNE, - JGE, - IMUL, - IDIV, - DIV, - SHR, - SETB, - SETBE, - SHL, - SAR, - NOT, - SETE, - SETNE, - SETL, - SETLE, - LEA, - MOVSX, - MOVSXD, - MOVZX, - CMP, - AND, - OR, - XOR, + // Atomic operations, prefetching and inline assembly. + CMPXCHG, + XCHG, + MFENCE, + PREFETCHNTA, + PREFETCHT0, + PREFETCHT1, + PREFETCHT2, + InlineAsm, } use InstOperator::*; @@ -151,31 +159,34 @@ impl InstOperator { #[derive(Copy, Clone, Debug)] pub enum Register { - Xmm(usize), - R11, - DL, - + // General registers and their available subregisters. RAX, EAX, + AX, + AL, RBX, RCX, ECX, CX, + CL, RDX, + DL, RSI, RDI, - RBP, - RSP, EDI, DI, DIL, - AX, - AL, - CL, - RIP, R8, R9, R10, + R11, + + // Stack, frame and instruction pointers. + RSP, + RBP, + RIP, + + Xmm(usize), } use Register::*; @@ -216,17 +227,19 @@ impl Display for Register { } pub enum InstOperand { - InlineAsmTemplate(String), Reg(Register), Num(usize), - Label(String), Str(&'static str), + Label(String), + Ptr(Register, usize), PtrOffset(Register, usize, usize), PtrAdd(Register, String), - ElseFlag(usize), + BeginFlag(usize), + ElseFlag(usize), EndFlag(usize), + InlineAsmTemplate(String), } impl Display for InstOperand { diff --git a/src/global.rs b/src/global.rs index f202499..06ff6e2 100644 --- a/src/global.rs +++ b/src/global.rs @@ -4,13 +4,14 @@ use std::collections::HashMap; pub struct GlobalVariable { pub ty: Type, pub alignment: usize, - pub thread_local: bool, - pub weak_import: bool, - pub hidden: bool, - pub common: bool, + pub data: Option, + pub internal: bool, pub declaration_only: bool, - pub data: Option, + pub common: bool, + pub weak_import: bool, + pub hidden: bool, + pub thread_local: bool, } #[derive(Debug, PartialEq)] pub enum GlobalVariableData { diff --git a/src/node.rs b/src/node.rs index 5ae1281..b31268d 100644 --- a/src/node.rs +++ b/src/node.rs @@ -25,51 +25,46 @@ pub enum AtomicOperation { #[derive(Debug, PartialEq, Clone)] pub enum NodeType { - InlineAsm(bool, bool), - Prefetch(bool, u8), - Atomic(AtomicOperation), - VaStart, - VaEnd, - Assign, - Cast, - FloatAbs, - StackAlloc, - BitScan { - bits: u8, - leading: bool, - }, - ZeroFill { - bytes: usize, - }, - /// Copy storage once, retaining the destination address as the result. - CopyBytes { - bytes: usize, - }, + // Values and conversions. + Num, LocalVar, GlobalVar, + Addr, + Deref, + Cast, + + // Arithmetic, bitwise and logical expressions. Add, - AddOverflow, - SubOverflow, Sub, Mul, Div, Mod, - Eq, - Ne, - Lt, - Le, BitLeft, BitRight, BitAnd, BitXor, BitOr, BitNot, + Eq, + Ne, + Lt, + Le, LogicalAnd, LogicalOr, - Num, + Conditional, + + // Assignments, calls and expression sequencing. + Assign, + SuffixIncr, + SuffixDecr, + CallFunc, + Sequence, + + // Declarations and control flow. + DefVar, + Block, Return, If, - Conditional, While, DoWhile, For, @@ -79,14 +74,28 @@ pub enum NodeType { Continue, Goto, Label, - Block, - Sequence, - CallFunc, - Addr, - Deref, - DefVar, - SuffixIncr, - SuffixDecr, + + // Builtins and lowered operations. + AddOverflow, + SubOverflow, + FloatAbs, + BitScan { + bits: u8, + leading: bool, + }, + VaStart, + VaEnd, + StackAlloc, + Prefetch(bool, u8), + Atomic(AtomicOperation), + InlineAsm(bool, bool), + ZeroFill { + bytes: usize, + }, + /// Copy storage once, retaining the destination address as the result. + CopyBytes { + bytes: usize, + }, } impl NodeType { @@ -105,21 +114,12 @@ impl Default for NodeType { #[derive(Default, Clone)] pub struct Node { pub nt: NodeType, - pub bit_field: Option, - /// Preserve lowered member identity for closest-subobject size queries. - pub member_access: bool, - pub compound_literal: bool, - pub volatile_access: bool, - pub registered: bool, - pub thread_local: bool, + pub token: Option, /// Stored object/call/cast type, or an inferred scalar arithmetic result. pub cty: Option, - pub token: Option, + pub lhs: Option>, pub rhs: Option>, - // for number - pub value: Option, - pub extended_float: Option>, // for "if", "for", "while" statement pub cond: Option>, pub then: Option>, @@ -130,19 +130,34 @@ pub struct Node { pub upd: Option>, // For blocks, local definitions, or inline assembly output destinations. pub children: Vec, - // flag of global variables + + // for number + pub value: Option, + pub extended_float: Option>, + + // Global symbol name. pub global_name: String, /// String symbol name, or the validated source template for inline assembly. pub dest: String, + // Function arguments or inline assembly inputs. pub args: Vec, pub parameter_types: Vec, pub variadic: bool, - pub vla_scope: Vec, + // for declaration of local variables pub offset: Option, /// Storage alignment requested by the local object declaration. pub alignment: usize, + pub bit_field: Option, + pub vla_scope: Vec, + + /// Preserve lowered member identity for closest-subobject size queries. + pub member_access: bool, + pub compound_literal: bool, + pub volatile_access: bool, + pub registered: bool, + pub thread_local: bool, } impl Node { diff --git a/src/preprocessor/native.rs b/src/preprocessor/native.rs index f707c7e..fdfa110 100644 --- a/src/preprocessor/native.rs +++ b/src/preprocessor/native.rs @@ -252,28 +252,37 @@ fn include_guard(source: &CleanSource) -> Option { } pub(super) struct Preprocessor { - macros: HashMap>, + // Search configuration and diagnostics. includes: Vec, - frameworks: Vec, quotes: Vec, - include_cache: HashMap<(PathBuf, SmolStr, bool, bool), Option>, + frameworks: Vec, + warnings_as_errors: bool, + dependency_only: bool, + + // Macro definitions and expansion dependencies. + macros: HashMap>, + builtin_names: HashSet, + stateful: HashSet, + // Keep historical edges after redefinition: extra expansion is safe; missing effects is not. + dependents: HashMap>, + + // Header lookup and source traversal. + include_context: PathBuf, search_origins: HashMap, + include_cache: HashMap<(PathBuf, SmolStr, bool, bool), Option>, headers: HashMap, once: HashSet, - counter: usize, depth: usize, - output: String, base_file: String, + + // Expansion state. + counter: usize, + condition_expansion: bool, + + // Generated source and dependency output. + output: String, pub(super) dependencies: Vec, seen_dependencies: HashSet, - warnings_as_errors: bool, - dependency_only: bool, - condition_expansion: bool, - stateful: HashSet, - // Keep historical edges after redefinition: extra expansion is safe; missing effects is not. - dependents: HashMap>, - builtin_names: HashSet, - include_context: PathBuf, } impl Preprocessor { diff --git a/src/token.rs b/src/token.rs index e013557..e0d6f09 100644 --- a/src/token.rs +++ b/src/token.rs @@ -81,9 +81,11 @@ pub struct Token { pub pos: usize, /// Active pragma pack limit at this source position. pub packing: Option, + pub s_value: smol_str::SmolStr, + pub i_value: usize, pub extended_float: Option>, - pub s_value: smol_str::SmolStr, + pub string_encoding: StringEncoding, pub string_bytes: StringBytes, }