From e6de4935d7500c2645ab3376a425ec1343be92e6 Mon Sep 17 00:00:00 2001 From: dytlzl Date: Sun, 18 Oct 2026 12:28:08 +0900 Subject: [PATCH 1/2] refactor: organize driver modules and definition order --- src/bin/{dynamite-cc.rs => dynamite-cc/main.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/bin/{dynamite-cc.rs => dynamite-cc/main.rs} (100%) diff --git a/src/bin/dynamite-cc.rs b/src/bin/dynamite-cc/main.rs similarity index 100% rename from src/bin/dynamite-cc.rs rename to src/bin/dynamite-cc/main.rs From a1f2b52a0c56b19259d4b397b1a37fb94d9da4d9 Mon Sep 17 00:00:00 2001 From: dytlzl Date: Sun, 18 Oct 2026 12:28:19 +0900 Subject: [PATCH 2/2] refactor: split driver parsing linking and shared definitions --- README.md | 3 + src/ast.rs | 110 ++++---- src/bin/dynamite-cc/arguments.rs | 320 +++++++++++++++++++++++ src/bin/dynamite-cc/llvm_tools.rs | 287 +------------------- src/bin/dynamite-cc/llvm_tools/darwin.rs | 149 +++++++++++ src/bin/dynamite-cc/llvm_tools/gnu.rs | 139 ++++++++++ src/bin/dynamite-cc/main.rs | 316 +--------------------- src/generator/mod.rs | 18 +- src/lib.rs | 3 +- src/node.rs | 78 +++--- 10 files changed, 722 insertions(+), 701 deletions(-) create mode 100644 src/bin/dynamite-cc/arguments.rs create mode 100644 src/bin/dynamite-cc/llvm_tools/darwin.rs create mode 100644 src/bin/dynamite-cc/llvm_tools/gnu.rs diff --git a/README.md b/README.md index b97aff6..a17d421 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ macro expansion and preprocessing/dependency arguments. Each backend keeps its generator entry point and core node dispatch in `src/generator//generator.rs`; `generator/aggregate.rs`, `globals.rs`, `atomic.rs` and `inline_asm.rs` contain the corresponding target-specific operations. +The C build driver starts in `src/bin/dynamite-cc/main.rs`, with argument handling +in `arguments.rs`. Its LLVM tools module keeps lowering and backend selection +in `llvm_tools.rs`, with native linking in `llvm_tools/gnu.rs` and `darwin.rs`. The image workflow runs only when `dockerfiles/dev.Dockerfile`, `.dockerignore`, or `.github/workflows/publish.yaml` changes, or when started manually. Image diff --git a/src/ast.rs b/src/ast.rs index 105361d..766fc22 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -28,6 +28,61 @@ mod validation; mod varargs; mod vla; +pub struct AstBuilderImpl<'a> { + 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, + current_function: Option, + mutable_static: Option, + return_pointer: Option, + variadic_parameters: Option>, + variadic_last: Option, + pending_bounds: Vec, + parameter_depth: usize, + declaration_attributes_state: Option, + dynamic_objects: Vec, + vla_active: Vec, + jump_depths: Vec, + continue_depths: HashMap, + switch_vla_depths: Vec, + next_enum_id: usize, + next_deferred_local: usize, + struct_definitions: Vec>>, + scope_stack: Vec>, + pub functions: HashMap, + pub global_variables: HashMap, + pub string_literals: Vec, + pub symbol_names: HashMap, +} + +pub struct ProgramAst<'a> { + pub functions: &'a HashMap, + pub global_variables: &'a HashMap, + pub string_literals: &'a Vec, + pub symbol_names: &'a HashMap, +} + +pub enum Identifier { + TypeDef(Type), + Constant(i64, Type), + EnumTag { + ty: Type, + fixed: bool, + defined: bool, + }, + StaticObject(Type, String), + Local(Type, usize, bool, usize), + Dynamic(Type, usize, bool), + Static(Type), +} + #[derive(Clone, Copy, PartialEq)] enum Nullability { Nullable, @@ -121,61 +176,6 @@ struct DeclarationAttributes { hidden: bool, } -pub struct AstBuilderImpl<'a> { - 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, - current_function: Option, - mutable_static: Option, - return_pointer: Option, - variadic_parameters: Option>, - variadic_last: Option, - pending_bounds: Vec, - parameter_depth: usize, - declaration_attributes_state: Option, - dynamic_objects: Vec, - vla_active: Vec, - jump_depths: Vec, - continue_depths: HashMap, - switch_vla_depths: Vec, - next_enum_id: usize, - next_deferred_local: usize, - struct_definitions: Vec>>, - scope_stack: Vec>, - pub functions: HashMap, - pub global_variables: HashMap, - pub string_literals: Vec, - pub symbol_names: HashMap, -} - -pub struct ProgramAst<'a> { - pub functions: &'a HashMap, - pub global_variables: &'a HashMap, - pub string_literals: &'a Vec, - pub symbol_names: &'a HashMap, -} - -pub enum Identifier { - TypeDef(Type), - Constant(i64, Type), - EnumTag { - ty: Type, - fixed: bool, - defined: bool, - }, - StaticObject(Type, String), - Local(Type, usize, bool, usize), - Dynamic(Type, usize, bool), - Static(Type), -} - pub(crate) const METADATA_ATTRIBUTES: &[&str] = &[ "availability", "deprecated", diff --git a/src/bin/dynamite-cc/arguments.rs b/src/bin/dynamite-cc/arguments.rs new file mode 100644 index 0000000..d14e737 --- /dev/null +++ b/src/bin/dynamite-cc/arguments.rs @@ -0,0 +1,320 @@ +//! Parse compiler arguments and preserve source/linker argument ordering. + +use super::{Arguments, Input, LinkArgument, Mode}; +use std::{ + collections::HashSet, + path::{Path, PathBuf}, +}; + +fn argument_value(args: &[String], index: &mut usize) -> Result { + *index += 1; + args.get(*index) + .cloned() + .ok_or_else(|| format!("{} requires an argument", args[*index - 1])) +} + +impl Arguments { + pub(super) fn parse(args: &[String]) -> Result { + let mut result = Self { + inputs: 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() + .any(|arg| matches!(arg.as_str(), "-E" | "-M" | "-MM")); + let mut c_language = false; + let mut index = 0; + while index < args.len() { + let arg = &args[index]; + let start = index; + let backend_start = result.backend.len(); + let input_start = result.inputs.len(); + match arg.as_str() { + "-j" | "--jobs" => { + result.jobs = argument_value(args, &mut index)? + .parse() + .map_err(|_| "jobs must be a positive integer")?; + } + _ if arg.starts_with("--jobs=") || arg.starts_with("-j") => { + let value = arg.strip_prefix("--jobs=").unwrap_or(&arg[2..]); + result.jobs = value + .parse() + .map_err(|_| "jobs must be a positive integer")?; + } + "-o" | "--output" => result.output = Some(argument_value(args, &mut index)?), + _ if arg.starts_with("--output=") => { + result.output = Some(arg[9..].into()); + } + _ if arg.starts_with("-o") && arg.len() > 2 => { + result.output = Some(arg[2..].into()); + } + "-c" => result.mode = Mode::Object, + "-S" => result.mode = Mode::Assembly, + "-emit-llvm" => { + result.emit_llvm = true; + result.backend.push(arg.clone()); + } + "-E" | "-M" | "-MM" => result.preprocess_only = true, + "-MD" | "-MMD" => result.dependencies = Some(arg == "-MD"), + "-MF" => result.dependency_file = Some(argument_value(args, &mut index)?), + "-MT" | "-MQ" => { + result.dependency_targets.push(arg.clone()); + result + .dependency_targets + .push(argument_value(args, &mut index)?); + } + "-MP" => result.phony_dependencies = true, + _ if ["-MF", "-MT", "-MQ"].iter().any(|p| arg.starts_with(p)) => { + let value = arg[3..].to_string(); + if arg.starts_with("-MF") { + result.dependency_file = Some(value); + } else { + result.dependency_targets.extend([arg[..3].into(), value]); + } + } + "-I" | "-D" | "-U" | "-F" | "-iframework" | "-isystem" | "-iquote" | "-include" + | "-imacros" | "-idirafter" | "-isysroot" | "--sysroot" => { + result.preprocessing.push(arg.clone()); + let value = argument_value(args, &mut index)?; + result.preprocessing.push(value.clone()); + if matches!( + arg.as_str(), + "-isysroot" | "--sysroot" | "-F" | "-iframework" + ) { + result.backend.extend([arg.clone(), value]); + } + } + "-x" => { + let language = argument_value(args, &mut index)?; + c_language = match language.as_str() { + "c" => true, + "none" => false, + _ => return Err(format!("unsupported input language: {language}")), + }; + } + _ if arg.starts_with("-x") && arg.len() > 2 => { + c_language = match &arg[2..] { + "c" => true, + "none" => false, + language => return Err(format!("unsupported input language: {language}")), + }; + } + "--" => return Err("use ./ prefixes for input names starting with '-'".into()), + "-target" | "--target" | "-arch" | "-m32" | "-fshort-enums" | "-fshort-wchar" + | "-funsigned-char" | "-fsigned-char" => { + return Err(format!("unsupported target or language option: {arg}")); + } + "-Xclang" | "-Xpreprocessor" | "-include-pch" | "-fsyntax-only" | "--compile" + | "--assemble" | "--preprocess" | "--analyze" | "--precompile" | "-emit-ast" + | "-fdriver-only" | "-###" => { + return Err(format!("unsupported frontend option: {arg}")); + } + "-L" + | "-l" + | "-framework" + | "-Xlinker" + | "-Xassembler" + | "-undefined" + | "-install_name" + | "-rpath" + | "-compatibility_version" + | "-current_version" => { + result.backend.push(arg.clone()); + result.backend.push(argument_value(args, &mut index)?); + } + "-" if !preprocess_only => { + return Err("C input from stdin is not supported; use a source file".into()); + } + _ if arg.starts_with('@') => return Err("response files are not supported".into()), + _ if arg.starts_with("--target=") || arg.starts_with("-fpack-struct") => { + return Err(format!("unsupported target or language option: {arg}")); + } + _ if arg.starts_with('-') => { + if arg.starts_with("-include-pch") + || arg.starts_with("-Xclang") + || arg.starts_with("-Xpreprocessor") + || arg.starts_with("-save-temps") + { + return Err(format!("unsupported frontend option: {arg}")); + } + if arg.starts_with("-Wp,") { + return Err("use explicit preprocessing options instead of -Wp,".into()); + } + if [ + "-I", + "-F", + "-iframework", + "-D", + "-U", + "-isystem", + "-iquote", + "-include", + "-imacros", + "-idirafter", + ] + .iter() + .any(|prefix| arg.starts_with(prefix)) + { + result.preprocessing.push(arg.clone()); + if arg.starts_with("-F") || arg.starts_with("-iframework") { + result.backend.push(arg.clone()); + } + } else if arg.starts_with("--sysroot=") || arg.starts_with("-isysroot") { + result.preprocessing.push(arg.clone()); + result.backend.push(arg.clone()); + } else { + if arg.starts_with("-O") + || arg.starts_with("-std=") + || arg == "-pthread" + || (arg.starts_with("-W") + && !arg.starts_with("-Wl,") + && !arg.starts_with("-Wa,")) + { + result.preprocessing.push(arg.clone()); + } + if !arg.starts_with("-std=") { + result.backend.push(arg.clone()); + } + } + } + _ => { + let extension = Path::new(arg).extension().and_then(|s| s.to_str()); + let source = c_language || matches!(extension, Some("c" | "i")); + if source && Path::new(arg).file_name().is_none() { + return Err(format!("invalid source path: {arg}")); + } + let versioned_library = Path::new(arg) + .file_name() + .and_then(|name| name.to_str()) + .and_then(|name| name.rsplit_once(".so.")) + .is_some_and(|(_, version)| { + version.split('.').all(|part| { + !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit()) + }) + }); + if !source + && !preprocess_only + && !matches!( + extension, + None | Some( + "s" | "S" + | "o" + | "obj" + | "a" + | "so" + | "dylib" + | "lib" + | "dll" + | "ll" + | "bc" + | "ld" + | "lds" + | "tbd" + ) + ) + && !versioned_library + { + return Err(format!("unsupported frontend input: {arg}")); + } + result.inputs.push(Input { + name: arg.clone(), + source, + }); + } + } + result.linker.extend( + result.backend[backend_start..] + .iter() + .cloned() + .map(LinkArgument::Option), + ); + if result.inputs.len() > input_start { + result.linker.push(LinkArgument::Input(input_start)); + } + if arg != "-j" + && arg != "--jobs" + && !arg.starts_with("--jobs=") + && !arg.starts_with("-j") + { + result.forwarded.extend_from_slice(&args[start..=index]); + } + index += 1; + } + if result.jobs == 0 { + return Err("jobs must be a positive integer".into()); + } + if result.preprocess_only { + return Ok(result); + } + let count = result.inputs.iter().filter(|input| input.source).count(); + if count > 0 && result.mode != Mode::Link && result.inputs.iter().any(|input| !input.source) + { + return Err("compile mixed C and non-C inputs in separate invocations".into()); + } + if count > 1 && result.mode != Mode::Link && result.output.is_some() { + return Err("cannot use one -o output with multiple compilation inputs".into()); + } + if count > 1 && result.dependency_file.is_some() { + return Err("multiple sources require separate default dependency files".into()); + } + if result.dependency_file.as_deref() == Some("-") && !result.preprocess_only { + return Err("dependency output to stdout is not supported when compiling".into()); + } + if result.mode != Mode::Link { + let mut outputs = HashSet::new(); + for input in result.inputs.iter().filter(|input| input.source) { + if !outputs.insert(result.output_for(input)) { + return Err("compilation inputs would overwrite the same output".into()); + } + } + } + Ok(result) + } + + pub(super) fn link_arguments(&self, outputs: &[PathBuf]) -> Vec { + let mut objects = outputs.iter(); + let mut arguments = vec![]; + for argument in &self.linker { + match argument { + LinkArgument::Option(option) => arguments.push(option.clone()), + LinkArgument::Input(index) => { + let input = &self.inputs[*index]; + arguments.push(if input.source { + objects.next().unwrap().to_string_lossy().into_owned() + } else { + input.name.clone() + }); + } + } + } + if let Some(output) = &self.output { + arguments.extend(["-o".into(), output.clone()]); + } + arguments + } + + pub(super) fn output_for(&self, input: &Input) -> PathBuf { + if let Some(output) = &self.output { + return output.into(); + } + let suffix = match (self.mode, self.emit_llvm) { + (Mode::Assembly, true) => "ll", + (Mode::Assembly, false) => "s", + (_, true) => "bc", + _ => "o", + }; + PathBuf::from(Path::new(&input.name).file_name().unwrap()).with_extension(suffix) + } +} diff --git a/src/bin/dynamite-cc/llvm_tools.rs b/src/bin/dynamite-cc/llvm_tools.rs index 074b02b..2298974 100644 --- a/src/bin/dynamite-cc/llvm_tools.rs +++ b/src/bin/dynamite-cc/llvm_tools.rs @@ -1,5 +1,10 @@ //! External LLVM tools for supported native GNU and Darwin toolchains. -use std::{fs, path::PathBuf, process::Command}; +mod darwin; +mod gnu; + +use darwin::link_macos; +use gnu::link_gnu; +use std::process::Command; use dynamite_compiler::timing::Timer; @@ -198,289 +203,9 @@ pub(super) fn lower( execute(&mut llc) } -fn gcc_runtime() -> Result { - if let Some(path) = std::env::var_os("DYNAMITE_GCC_RUNTIME") { - let path = PathBuf::from(path); - if path.join("crtbeginS.o").is_file() && path.join("crtendS.o").is_file() { - return Ok(path); - } - return Err("DYNAMITE_GCC_RUNTIME must contain crtbeginS.o and crtendS.o".into()); - } - let mut versions = fs::read_dir("/usr/lib/gcc/x86_64-linux-gnu") - .map_err(|error| { - format!("cannot find GNU runtime files; set DYNAMITE_GCC_RUNTIME: {error}") - })? - .filter_map(Result::ok) - .filter_map(|entry| { - let version = entry - .file_name() - .to_string_lossy() - .split('.') - .map(str::parse::) - .collect::, _>>() - .ok()?; - let path = entry.path(); - (path.join("crtbeginS.o").is_file() && path.join("crtendS.o").is_file()) - .then_some((version, path)) - }) - .collect::>(); - versions.sort_by(|a, b| b.0.cmp(&a.0)); - versions - .into_iter() - .next() - .map(|(_, path)| path) - .ok_or_else(|| "cannot find GNU runtime files; set DYNAMITE_GCC_RUNTIME".into()) -} - pub(super) fn link(arguments: &[String]) -> Result<(), String> { if cfg!(target_os = "macos") { return link_macos(arguments); } link_gnu(arguments) } - -fn link_gnu(arguments: &[String]) -> Result<(), String> { - let runtime = gcc_runtime()?; - let system = PathBuf::from("/usr/lib/x86_64-linux-gnu"); - let shared = arguments.iter().any(|arg| arg == "-shared"); - let pie = !shared - && arguments - .iter() - .rev() - .find(|arg| matches!(arg.as_str(), "-pie" | "-no-pie")) - .is_none_or(|arg| arg != "-no-pie"); - let mut command = tool("DYNAMITE_LLD", "ld.lld"); - command.arg("--eh-frame-hdr"); - if shared { - command.arg("-shared"); - } else { - if pie { - command.arg("-pie"); - } - command.args(["-dynamic-linker", "/lib64/ld-linux-x86-64.so.2"]); - command.arg(system.join(if pie { "Scrt1.o" } else { "crt1.o" })); - } - command - .arg(system.join("crti.o")) - .arg(runtime.join(if shared || pie { - "crtbeginS.o" - } else { - "crtbegin.o" - })); - let mut index = 0; - while index < arguments.len() { - let argument = &arguments[index]; - match argument.as_str() { - "-o" | "-L" | "-l" | "-Xlinker" => { - if argument != "-Xlinker" { - command.arg(argument); - } - index += 1; - command.arg( - arguments - .get(index) - .ok_or_else(|| format!("{argument} requires an argument"))?, - ); - } - "-pthread" => { - command.arg("-lpthread"); - } - "-rdynamic" => { - command.arg("--export-dynamic"); - } - "-shared" | "-pie" | "-no-pie" => {} - _ if argument.starts_with("-Wl,") => { - command.args(argument[4..].split(',')); - } - _ if argument.starts_with("-O") - || (argument.starts_with("-W") && !argument.starts_with("-Wa,")) - || matches!( - argument.as_str(), - "-g" | "-g0" - | "-ggdb" - | "-fPIC" - | "-fpic" - | "-fPIE" - | "-fpie" - | "-fomit-frame-pointer" - | "-fno-omit-frame-pointer" - ) => {} - _ if argument.starts_with("-L") || argument.starts_with("-l") || argument == "-s" => { - command.arg(argument); - } - _ if argument.starts_with('-') => { - return Err(format!("unsupported LLVM tools linker option: {argument}")); - } - _ => { - if !matches!( - std::path::Path::new(argument) - .extension() - .and_then(|part| part.to_str()), - Some("o" | "a" | "so") - ) && !argument.contains(".so.") - { - return Err(format!("unsupported LLVM tools linker input: {argument}")); - } - command.arg(argument); - } - } - index += 1; - } - command - .arg(format!("-L{}", runtime.display())) - .arg("-L/usr/lib/x86_64-linux-gnu") - .arg("-L/lib/x86_64-linux-gnu") - .args(["-lc", "-lgcc", "--as-needed", "-lgcc_s", "--no-as-needed"]) - .arg(runtime.join(if shared || pie { - "crtendS.o" - } else { - "crtend.o" - })) - .arg(system.join("crtn.o")); - execute(&mut command) -} - -fn link_macos(arguments: &[String]) -> Result<(), String> { - let mut sdk = None; - for (index, argument) in arguments.iter().enumerate() { - if argument == "-isysroot" { - sdk = Some(PathBuf::from( - arguments - .get(index + 1) - .ok_or("-isysroot requires an argument")?, - )); - } else if let Some(root) = argument.strip_prefix("-isysroot") { - sdk = Some(PathBuf::from(root)); - } - } - let sdk = sdk - .map(Ok) - .unwrap_or_else(dynamite_compiler::toolchain::macos_sdk_root)?; - let version = Command::new("/usr/bin/xcrun") - .arg("--sdk") - .arg(&sdk) - .arg("--show-sdk-version") - .output() - .map_err(|e| format!("cannot read macOS SDK version: {e}"))?; - if !version.status.success() { - return Err("cannot read macOS SDK version".into()); - } - let version = String::from_utf8(version.stdout).map_err(|e| e.to_string())?; - dynamite_compiler::toolchain::macos_version_number(version.trim())?; - let minimum = dynamite_compiler::toolchain::macos_deployment_target()?; - let mut command = tool("DYNAMITE_LD", "/usr/bin/ld"); - command - .args([ - "-arch", - if cfg!(target_arch = "aarch64") { - "arm64" - } else { - "x86_64" - }, - ]) - .arg("-syslibroot") - .arg(&sdk) - .args(["-platform_version", "macos", &minimum, version.trim()]); - let dylib = arguments - .iter() - .any(|a| matches!(a.as_str(), "-shared" | "-dynamiclib")); - let bundle = arguments.iter().any(|a| a == "-bundle"); - if dylib && bundle { - return Err("cannot combine a dynamic library and a bundle".into()); - } - if dylib { - command.arg("-dylib"); - } - if bundle { - command.arg("-bundle"); - } - let mut index = 0; - while index < arguments.len() { - let argument = &arguments[index]; - match argument.as_str() { - "-o" - | "-L" - | "-l" - | "-framework" - | "-F" - | "-iframework" - | "-Xlinker" - | "-undefined" - | "-install_name" - | "-rpath" - | "-compatibility_version" - | "-current_version" - | "-isysroot" => { - let value = arguments - .get(index + 1) - .ok_or_else(|| format!("{argument} requires an argument"))?; - if argument != "-isysroot" { - if argument != "-Xlinker" { - command.arg(if argument == "-iframework" { - "-F" - } else { - argument - }); - } - command.arg(value); - } - index += 1; - } - "-shared" | "-dynamiclib" | "-bundle" | "-dynamic" | "-pthread" => {} - "-rdynamic" => { - command.arg("-export_dynamic"); - } - "-no-pie" => { - command.arg("-no_pie"); - } - "-pie" | "-s" => { - command.arg(argument); - } - _ if argument.starts_with("-Wl,") => { - command.args(argument[4..].split(',')); - } - _ if argument.starts_with("-iframework") => { - command.arg("-F").arg(&argument[11..]); - } - _ if argument.starts_with("-isysroot") - || argument.starts_with("-O") - || (argument.starts_with("-W") && !argument.starts_with("-Wa,")) - || matches!( - argument.as_str(), - "-g" | "-g0" - | "-ggdb" - | "-fPIC" - | "-fpic" - | "-fPIE" - | "-fpie" - | "-fomit-frame-pointer" - | "-fno-omit-frame-pointer" - | "-fno-common" - | "-fno-strict-aliasing" - ) => {} - _ if argument.starts_with("-F") - || argument.starts_with("-L") - || argument.starts_with("-l") => - { - command.arg(argument); - } - _ if argument.starts_with('-') => { - return Err(format!("unsupported LLVM tools linker option: {argument}")); - } - _ => { - if !matches!( - std::path::Path::new(argument) - .extension() - .and_then(|s| s.to_str()), - Some("o" | "a" | "dylib" | "tbd") - ) { - return Err(format!("unsupported LLVM tools linker input: {argument}")); - } - command.arg(argument); - } - } - index += 1; - } - command.arg("-lSystem"); - execute(&mut command) -} diff --git a/src/bin/dynamite-cc/llvm_tools/darwin.rs b/src/bin/dynamite-cc/llvm_tools/darwin.rs new file mode 100644 index 0000000..7823c7d --- /dev/null +++ b/src/bin/dynamite-cc/llvm_tools/darwin.rs @@ -0,0 +1,149 @@ +//! Native Darwin SDK and linker arguments. + +use super::{execute, tool}; +use std::{path::PathBuf, process::Command}; + +pub(super) fn link_macos(arguments: &[String]) -> Result<(), String> { + let mut sdk = None; + for (index, argument) in arguments.iter().enumerate() { + if argument == "-isysroot" { + sdk = Some(PathBuf::from( + arguments + .get(index + 1) + .ok_or("-isysroot requires an argument")?, + )); + } else if let Some(root) = argument.strip_prefix("-isysroot") { + sdk = Some(PathBuf::from(root)); + } + } + let sdk = sdk + .map(Ok) + .unwrap_or_else(dynamite_compiler::toolchain::macos_sdk_root)?; + let version = Command::new("/usr/bin/xcrun") + .arg("--sdk") + .arg(&sdk) + .arg("--show-sdk-version") + .output() + .map_err(|e| format!("cannot read macOS SDK version: {e}"))?; + if !version.status.success() { + return Err("cannot read macOS SDK version".into()); + } + let version = String::from_utf8(version.stdout).map_err(|e| e.to_string())?; + dynamite_compiler::toolchain::macos_version_number(version.trim())?; + let minimum = dynamite_compiler::toolchain::macos_deployment_target()?; + let mut command = tool("DYNAMITE_LD", "/usr/bin/ld"); + command + .args([ + "-arch", + if cfg!(target_arch = "aarch64") { + "arm64" + } else { + "x86_64" + }, + ]) + .arg("-syslibroot") + .arg(&sdk) + .args(["-platform_version", "macos", &minimum, version.trim()]); + let dylib = arguments + .iter() + .any(|a| matches!(a.as_str(), "-shared" | "-dynamiclib")); + let bundle = arguments.iter().any(|a| a == "-bundle"); + if dylib && bundle { + return Err("cannot combine a dynamic library and a bundle".into()); + } + if dylib { + command.arg("-dylib"); + } + if bundle { + command.arg("-bundle"); + } + let mut index = 0; + while index < arguments.len() { + let argument = &arguments[index]; + match argument.as_str() { + "-o" + | "-L" + | "-l" + | "-framework" + | "-F" + | "-iframework" + | "-Xlinker" + | "-undefined" + | "-install_name" + | "-rpath" + | "-compatibility_version" + | "-current_version" + | "-isysroot" => { + let value = arguments + .get(index + 1) + .ok_or_else(|| format!("{argument} requires an argument"))?; + if argument != "-isysroot" { + if argument != "-Xlinker" { + command.arg(if argument == "-iframework" { + "-F" + } else { + argument + }); + } + command.arg(value); + } + index += 1; + } + "-shared" | "-dynamiclib" | "-bundle" | "-dynamic" | "-pthread" => {} + "-rdynamic" => { + command.arg("-export_dynamic"); + } + "-no-pie" => { + command.arg("-no_pie"); + } + "-pie" | "-s" => { + command.arg(argument); + } + _ if argument.starts_with("-Wl,") => { + command.args(argument[4..].split(',')); + } + _ if argument.starts_with("-iframework") => { + command.arg("-F").arg(&argument[11..]); + } + _ if argument.starts_with("-isysroot") + || argument.starts_with("-O") + || (argument.starts_with("-W") && !argument.starts_with("-Wa,")) + || matches!( + argument.as_str(), + "-g" | "-g0" + | "-ggdb" + | "-fPIC" + | "-fpic" + | "-fPIE" + | "-fpie" + | "-fomit-frame-pointer" + | "-fno-omit-frame-pointer" + | "-fno-common" + | "-fno-strict-aliasing" + ) => {} + _ if argument.starts_with("-F") + || argument.starts_with("-L") + || argument.starts_with("-l") => + { + command.arg(argument); + } + _ if argument.starts_with('-') => { + return Err(format!("unsupported LLVM tools linker option: {argument}")); + } + _ => { + if !matches!( + std::path::Path::new(argument) + .extension() + .and_then(|s| s.to_str()), + Some("o" | "a" | "dylib" | "tbd") + ) { + return Err(format!("unsupported LLVM tools linker input: {argument}")); + } + command.arg(argument); + } + } + index += 1; + } + command.arg("-lSystem"); + execute(&mut command) +} diff --git a/src/bin/dynamite-cc/llvm_tools/gnu.rs b/src/bin/dynamite-cc/llvm_tools/gnu.rs new file mode 100644 index 0000000..250cd92 --- /dev/null +++ b/src/bin/dynamite-cc/llvm_tools/gnu.rs @@ -0,0 +1,139 @@ +//! GNU runtime discovery and native ELF linking. + +use super::{execute, tool}; +use std::{fs, path::PathBuf}; + +fn gcc_runtime() -> Result { + if let Some(path) = std::env::var_os("DYNAMITE_GCC_RUNTIME") { + let path = PathBuf::from(path); + if path.join("crtbeginS.o").is_file() && path.join("crtendS.o").is_file() { + return Ok(path); + } + return Err("DYNAMITE_GCC_RUNTIME must contain crtbeginS.o and crtendS.o".into()); + } + let mut versions = fs::read_dir("/usr/lib/gcc/x86_64-linux-gnu") + .map_err(|error| { + format!("cannot find GNU runtime files; set DYNAMITE_GCC_RUNTIME: {error}") + })? + .filter_map(Result::ok) + .filter_map(|entry| { + let version = entry + .file_name() + .to_string_lossy() + .split('.') + .map(str::parse::) + .collect::, _>>() + .ok()?; + let path = entry.path(); + (path.join("crtbeginS.o").is_file() && path.join("crtendS.o").is_file()) + .then_some((version, path)) + }) + .collect::>(); + versions.sort_by(|a, b| b.0.cmp(&a.0)); + versions + .into_iter() + .next() + .map(|(_, path)| path) + .ok_or_else(|| "cannot find GNU runtime files; set DYNAMITE_GCC_RUNTIME".into()) +} + +pub(super) fn link_gnu(arguments: &[String]) -> Result<(), String> { + let runtime = gcc_runtime()?; + let system = PathBuf::from("/usr/lib/x86_64-linux-gnu"); + let shared = arguments.iter().any(|arg| arg == "-shared"); + let pie = !shared + && arguments + .iter() + .rev() + .find(|arg| matches!(arg.as_str(), "-pie" | "-no-pie")) + .is_none_or(|arg| arg != "-no-pie"); + let mut command = tool("DYNAMITE_LLD", "ld.lld"); + command.arg("--eh-frame-hdr"); + if shared { + command.arg("-shared"); + } else { + if pie { + command.arg("-pie"); + } + command.args(["-dynamic-linker", "/lib64/ld-linux-x86-64.so.2"]); + command.arg(system.join(if pie { "Scrt1.o" } else { "crt1.o" })); + } + command + .arg(system.join("crti.o")) + .arg(runtime.join(if shared || pie { + "crtbeginS.o" + } else { + "crtbegin.o" + })); + let mut index = 0; + while index < arguments.len() { + let argument = &arguments[index]; + match argument.as_str() { + "-o" | "-L" | "-l" | "-Xlinker" => { + if argument != "-Xlinker" { + command.arg(argument); + } + index += 1; + command.arg( + arguments + .get(index) + .ok_or_else(|| format!("{argument} requires an argument"))?, + ); + } + "-pthread" => { + command.arg("-lpthread"); + } + "-rdynamic" => { + command.arg("--export-dynamic"); + } + "-shared" | "-pie" | "-no-pie" => {} + _ if argument.starts_with("-Wl,") => { + command.args(argument[4..].split(',')); + } + _ if argument.starts_with("-O") + || (argument.starts_with("-W") && !argument.starts_with("-Wa,")) + || matches!( + argument.as_str(), + "-g" | "-g0" + | "-ggdb" + | "-fPIC" + | "-fpic" + | "-fPIE" + | "-fpie" + | "-fomit-frame-pointer" + | "-fno-omit-frame-pointer" + ) => {} + _ if argument.starts_with("-L") || argument.starts_with("-l") || argument == "-s" => { + command.arg(argument); + } + _ if argument.starts_with('-') => { + return Err(format!("unsupported LLVM tools linker option: {argument}")); + } + _ => { + if !matches!( + std::path::Path::new(argument) + .extension() + .and_then(|part| part.to_str()), + Some("o" | "a" | "so") + ) && !argument.contains(".so.") + { + return Err(format!("unsupported LLVM tools linker input: {argument}")); + } + command.arg(argument); + } + } + index += 1; + } + command + .arg(format!("-L{}", runtime.display())) + .arg("-L/usr/lib/x86_64-linux-gnu") + .arg("-L/lib/x86_64-linux-gnu") + .args(["-lc", "-lgcc", "--as-needed", "-lgcc_s", "--no-as-needed"]) + .arg(runtime.join(if shared || pie { + "crtendS.o" + } else { + "crtend.o" + })) + .arg(system.join("crtn.o")); + execute(&mut command) +} diff --git a/src/bin/dynamite-cc/main.rs b/src/bin/dynamite-cc/main.rs index 916b996..dd71b73 100644 --- a/src/bin/dynamite-cc/main.rs +++ b/src/bin/dynamite-cc/main.rs @@ -1,12 +1,11 @@ //! Native C build driver with external Clang or LLVM tools. -#[path = "dynamite-cc/llvm_tools.rs"] +mod arguments; mod llvm_tools; use dynamite_compiler::timing::Timer; use std::{ borrow::Cow, - collections::HashSet, fs, path::{Path, PathBuf}, process::Command, @@ -47,319 +46,6 @@ struct Arguments { emit_llvm: bool, } -fn argument_value(args: &[String], index: &mut usize) -> Result { - *index += 1; - args.get(*index) - .cloned() - .ok_or_else(|| format!("{} requires an argument", args[*index - 1])) -} - -impl Arguments { - fn parse(args: &[String]) -> Result { - let mut result = Self { - inputs: 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() - .any(|arg| matches!(arg.as_str(), "-E" | "-M" | "-MM")); - let mut c_language = false; - let mut index = 0; - while index < args.len() { - let arg = &args[index]; - let start = index; - let backend_start = result.backend.len(); - let input_start = result.inputs.len(); - match arg.as_str() { - "-j" | "--jobs" => { - result.jobs = argument_value(args, &mut index)? - .parse() - .map_err(|_| "jobs must be a positive integer")?; - } - _ if arg.starts_with("--jobs=") || arg.starts_with("-j") => { - let value = arg.strip_prefix("--jobs=").unwrap_or(&arg[2..]); - result.jobs = value - .parse() - .map_err(|_| "jobs must be a positive integer")?; - } - "-o" | "--output" => result.output = Some(argument_value(args, &mut index)?), - _ if arg.starts_with("--output=") => { - result.output = Some(arg[9..].into()); - } - _ if arg.starts_with("-o") && arg.len() > 2 => { - result.output = Some(arg[2..].into()); - } - "-c" => result.mode = Mode::Object, - "-S" => result.mode = Mode::Assembly, - "-emit-llvm" => { - result.emit_llvm = true; - result.backend.push(arg.clone()); - } - "-E" | "-M" | "-MM" => result.preprocess_only = true, - "-MD" | "-MMD" => result.dependencies = Some(arg == "-MD"), - "-MF" => result.dependency_file = Some(argument_value(args, &mut index)?), - "-MT" | "-MQ" => { - result.dependency_targets.push(arg.clone()); - result - .dependency_targets - .push(argument_value(args, &mut index)?); - } - "-MP" => result.phony_dependencies = true, - _ if ["-MF", "-MT", "-MQ"].iter().any(|p| arg.starts_with(p)) => { - let value = arg[3..].to_string(); - if arg.starts_with("-MF") { - result.dependency_file = Some(value); - } else { - result.dependency_targets.extend([arg[..3].into(), value]); - } - } - "-I" | "-D" | "-U" | "-F" | "-iframework" | "-isystem" | "-iquote" | "-include" - | "-imacros" | "-idirafter" | "-isysroot" | "--sysroot" => { - result.preprocessing.push(arg.clone()); - let value = argument_value(args, &mut index)?; - result.preprocessing.push(value.clone()); - if matches!( - arg.as_str(), - "-isysroot" | "--sysroot" | "-F" | "-iframework" - ) { - result.backend.extend([arg.clone(), value]); - } - } - "-x" => { - let language = argument_value(args, &mut index)?; - c_language = match language.as_str() { - "c" => true, - "none" => false, - _ => return Err(format!("unsupported input language: {language}")), - }; - } - _ if arg.starts_with("-x") && arg.len() > 2 => { - c_language = match &arg[2..] { - "c" => true, - "none" => false, - language => return Err(format!("unsupported input language: {language}")), - }; - } - "--" => return Err("use ./ prefixes for input names starting with '-'".into()), - "-target" | "--target" | "-arch" | "-m32" | "-fshort-enums" | "-fshort-wchar" - | "-funsigned-char" | "-fsigned-char" => { - return Err(format!("unsupported target or language option: {arg}")); - } - "-Xclang" | "-Xpreprocessor" | "-include-pch" | "-fsyntax-only" | "--compile" - | "--assemble" | "--preprocess" | "--analyze" | "--precompile" | "-emit-ast" - | "-fdriver-only" | "-###" => { - return Err(format!("unsupported frontend option: {arg}")); - } - "-L" - | "-l" - | "-framework" - | "-Xlinker" - | "-Xassembler" - | "-undefined" - | "-install_name" - | "-rpath" - | "-compatibility_version" - | "-current_version" => { - result.backend.push(arg.clone()); - result.backend.push(argument_value(args, &mut index)?); - } - "-" if !preprocess_only => { - return Err("C input from stdin is not supported; use a source file".into()); - } - _ if arg.starts_with('@') => return Err("response files are not supported".into()), - _ if arg.starts_with("--target=") || arg.starts_with("-fpack-struct") => { - return Err(format!("unsupported target or language option: {arg}")); - } - _ if arg.starts_with('-') => { - if arg.starts_with("-include-pch") - || arg.starts_with("-Xclang") - || arg.starts_with("-Xpreprocessor") - || arg.starts_with("-save-temps") - { - return Err(format!("unsupported frontend option: {arg}")); - } - if arg.starts_with("-Wp,") { - return Err("use explicit preprocessing options instead of -Wp,".into()); - } - if [ - "-I", - "-F", - "-iframework", - "-D", - "-U", - "-isystem", - "-iquote", - "-include", - "-imacros", - "-idirafter", - ] - .iter() - .any(|prefix| arg.starts_with(prefix)) - { - result.preprocessing.push(arg.clone()); - if arg.starts_with("-F") || arg.starts_with("-iframework") { - result.backend.push(arg.clone()); - } - } else if arg.starts_with("--sysroot=") || arg.starts_with("-isysroot") { - result.preprocessing.push(arg.clone()); - result.backend.push(arg.clone()); - } else { - if arg.starts_with("-O") - || arg.starts_with("-std=") - || arg == "-pthread" - || (arg.starts_with("-W") - && !arg.starts_with("-Wl,") - && !arg.starts_with("-Wa,")) - { - result.preprocessing.push(arg.clone()); - } - if !arg.starts_with("-std=") { - result.backend.push(arg.clone()); - } - } - } - _ => { - let extension = Path::new(arg).extension().and_then(|s| s.to_str()); - let source = c_language || matches!(extension, Some("c" | "i")); - if source && Path::new(arg).file_name().is_none() { - return Err(format!("invalid source path: {arg}")); - } - let versioned_library = Path::new(arg) - .file_name() - .and_then(|name| name.to_str()) - .and_then(|name| name.rsplit_once(".so.")) - .is_some_and(|(_, version)| { - version.split('.').all(|part| { - !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit()) - }) - }); - if !source - && !preprocess_only - && !matches!( - extension, - None | Some( - "s" | "S" - | "o" - | "obj" - | "a" - | "so" - | "dylib" - | "lib" - | "dll" - | "ll" - | "bc" - | "ld" - | "lds" - | "tbd" - ) - ) - && !versioned_library - { - return Err(format!("unsupported frontend input: {arg}")); - } - result.inputs.push(Input { - name: arg.clone(), - source, - }); - } - } - result.linker.extend( - result.backend[backend_start..] - .iter() - .cloned() - .map(LinkArgument::Option), - ); - if result.inputs.len() > input_start { - result.linker.push(LinkArgument::Input(input_start)); - } - if arg != "-j" - && arg != "--jobs" - && !arg.starts_with("--jobs=") - && !arg.starts_with("-j") - { - result.forwarded.extend_from_slice(&args[start..=index]); - } - index += 1; - } - if result.jobs == 0 { - return Err("jobs must be a positive integer".into()); - } - if result.preprocess_only { - return Ok(result); - } - let count = result.inputs.iter().filter(|input| input.source).count(); - if count > 0 && result.mode != Mode::Link && result.inputs.iter().any(|input| !input.source) - { - return Err("compile mixed C and non-C inputs in separate invocations".into()); - } - if count > 1 && result.mode != Mode::Link && result.output.is_some() { - return Err("cannot use one -o output with multiple compilation inputs".into()); - } - if count > 1 && result.dependency_file.is_some() { - return Err("multiple sources require separate default dependency files".into()); - } - if result.dependency_file.as_deref() == Some("-") && !result.preprocess_only { - return Err("dependency output to stdout is not supported when compiling".into()); - } - if result.mode != Mode::Link { - let mut outputs = HashSet::new(); - for input in result.inputs.iter().filter(|input| input.source) { - if !outputs.insert(result.output_for(input)) { - return Err("compilation inputs would overwrite the same output".into()); - } - } - } - Ok(result) - } - - fn link_arguments(&self, outputs: &[PathBuf]) -> Vec { - let mut objects = outputs.iter(); - let mut arguments = vec![]; - for argument in &self.linker { - match argument { - LinkArgument::Option(option) => arguments.push(option.clone()), - LinkArgument::Input(index) => { - let input = &self.inputs[*index]; - arguments.push(if input.source { - objects.next().unwrap().to_string_lossy().into_owned() - } else { - input.name.clone() - }); - } - } - } - if let Some(output) = &self.output { - arguments.extend(["-o".into(), output.clone()]); - } - arguments - } - - fn output_for(&self, input: &Input) -> PathBuf { - if let Some(output) = &self.output { - return output.into(); - } - let suffix = match (self.mode, self.emit_llvm) { - (Mode::Assembly, true) => "ll", - (Mode::Assembly, false) => "s", - (_, true) => "bc", - _ => "o", - }; - PathBuf::from(Path::new(&input.name).file_name().unwrap()).with_extension(suffix) - } -} - struct TemporaryDirectory(PathBuf); impl TemporaryDirectory { fn new() -> Result { diff --git a/src/generator/mod.rs b/src/generator/mod.rs index 7d88502..2724dde 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -1,3 +1,5 @@ +use crate::{ast::ProgramAst, error::ErrorLogger}; + pub mod aarch64; pub mod abi; pub mod llvm; @@ -14,6 +16,13 @@ pub enum Arch { X86_64, } +#[derive(Clone, Copy)] +pub enum ScalarLocation { + General(usize), + Floating(usize), + Stack(usize, usize), +} + /// Register width requested by the supported GNU target operand modifiers. pub(crate) fn asm_register_width( arch: Arch, @@ -30,8 +39,6 @@ pub(crate) fn asm_register_width( } } -use crate::{ast::ProgramAst, error::ErrorLogger}; - /// References reachable from emitted definitions and global initializers. /// Unreferenced static inline bodies must not create linker dependencies. pub(crate) fn referenced_symbols<'a>( @@ -132,13 +139,6 @@ pub fn new<'a>( } } -#[derive(Clone, Copy)] -pub enum ScalarLocation { - General(usize), - Floating(usize), - Stack(usize, usize), -} - pub fn scalar_arguments( types: &[crate::ctype::Type], general_limit: usize, diff --git a/src/lib.rs b/src/lib.rs index a59960e..6f07f9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod preprocessor; pub mod timing; pub mod token; pub mod tokenizer; +pub mod toolchain; pub mod trie; use ast::{AstBuilder, ProgramAst}; @@ -156,5 +157,3 @@ fn gen_asm(ast: ProgramAst, error_printer: &dyn error::ErrorLogger) -> String { fn gen_llvm_ir(ast: ProgramAst, error_printer: &dyn error::ErrorLogger) -> String { generator::llvm::generator::IrGenerator::new(error_printer).generate(ast) } - -pub mod toolchain; diff --git a/src/node.rs b/src/node.rs index 621c626..5ae1281 100644 --- a/src/node.rs +++ b/src/node.rs @@ -23,45 +23,6 @@ pub enum AtomicOperation { CompareExchange(bool), } -/// Decode GNU numbered register operands, leaving target-specific modifiers to the backend. -pub(crate) fn render_inline_assembly( - template: &str, - mut operand: impl FnMut(usize, Option) -> Option, -) -> Option { - let mut rendered = String::new(); - let mut chars = template.chars().peekable(); - while let Some(ch) = chars.next() { - if ch != '%' { - rendered.push(ch); - continue; - } - if chars.peek() == Some(&'%') { - chars.next(); - rendered.push('%'); - continue; - } - let modifier = chars - .peek() - .copied() - .filter(char::is_ascii_alphabetic) - .inspect(|_| { - chars.next(); - }); - let mut index = None; - while let Some(ch) = chars.peek().copied().filter(char::is_ascii_digit) { - chars.next(); - index = Some( - index - .unwrap_or(0usize) - .checked_mul(10)? - .checked_add(ch.to_digit(10)? as usize)?, - ); - } - rendered.push_str(&operand(index?, modifier)?); - } - Some(rendered) -} - #[derive(Debug, PartialEq, Clone)] pub enum NodeType { InlineAsm(bool, bool), @@ -671,6 +632,45 @@ impl std::fmt::Debug for Node { } } +/// Decode GNU numbered register operands, leaving target-specific modifiers to the backend. +pub(crate) fn render_inline_assembly( + template: &str, + mut operand: impl FnMut(usize, Option) -> Option, +) -> Option { + let mut rendered = String::new(); + let mut chars = template.chars().peekable(); + while let Some(ch) = chars.next() { + if ch != '%' { + rendered.push(ch); + continue; + } + if chars.peek() == Some(&'%') { + chars.next(); + rendered.push('%'); + continue; + } + let modifier = chars + .peek() + .copied() + .filter(char::is_ascii_alphabetic) + .inspect(|_| { + chars.next(); + }); + let mut index = None; + while let Some(ch) = chars.peek().copied().filter(char::is_ascii_digit) { + chars.next(); + index = Some( + index + .unwrap_or(0usize) + .checked_mul(10)? + .checked_add(ch.to_digit(10)? as usize)?, + ); + } + rendered.push_str(&operand(index?, modifier)?); + } + Some(rendered) +} + #[cfg(test)] mod tests { use super::*;