From 8b3e6f5ef38f481672680fb5df2008117df72e36 Mon Sep 17 00:00:00 2001 From: Edward Gonzalez Date: Thu, 6 Aug 2026 17:08:56 -0400 Subject: [PATCH] wip: rusty with the build scripts --- {duffle => code/duffle}/Readme.md | 0 {duffle => code/duffle}/dsl.h | 0 {duffle => code/duffle}/encoding.h | 0 {duffle => code/duffle}/hashing.h | 0 {duffle => code/duffle}/memory.h | 0 {duffle => code/duffle}/tables.h | 0 {duffle => code/duffle}/text.h | 0 {duffle => code/duffle}/win32.h | 0 code/part_1/sim_8086.c | 3 + scripts/build.ps1 | 139 +++++++++++++++++++++++++++-- scripts/helpers/misc.lua | 0 11 files changed, 133 insertions(+), 9 deletions(-) rename {duffle => code/duffle}/Readme.md (100%) rename {duffle => code/duffle}/dsl.h (100%) rename {duffle => code/duffle}/encoding.h (100%) rename {duffle => code/duffle}/hashing.h (100%) rename {duffle => code/duffle}/memory.h (100%) rename {duffle => code/duffle}/tables.h (100%) rename {duffle => code/duffle}/text.h (100%) rename {duffle => code/duffle}/win32.h (100%) create mode 100644 code/part_1/sim_8086.c delete mode 100644 scripts/helpers/misc.lua diff --git a/duffle/Readme.md b/code/duffle/Readme.md similarity index 100% rename from duffle/Readme.md rename to code/duffle/Readme.md diff --git a/duffle/dsl.h b/code/duffle/dsl.h similarity index 100% rename from duffle/dsl.h rename to code/duffle/dsl.h diff --git a/duffle/encoding.h b/code/duffle/encoding.h similarity index 100% rename from duffle/encoding.h rename to code/duffle/encoding.h diff --git a/duffle/hashing.h b/code/duffle/hashing.h similarity index 100% rename from duffle/hashing.h rename to code/duffle/hashing.h diff --git a/duffle/memory.h b/code/duffle/memory.h similarity index 100% rename from duffle/memory.h rename to code/duffle/memory.h diff --git a/duffle/tables.h b/code/duffle/tables.h similarity index 100% rename from duffle/tables.h rename to code/duffle/tables.h diff --git a/duffle/text.h b/code/duffle/text.h similarity index 100% rename from duffle/text.h rename to code/duffle/text.h diff --git a/duffle/win32.h b/code/duffle/win32.h similarity index 100% rename from duffle/win32.h rename to code/duffle/win32.h diff --git a/code/part_1/sim_8086.c b/code/part_1/sim_8086.c new file mode 100644 index 0000000..b28b04f --- /dev/null +++ b/code/part_1/sim_8086.c @@ -0,0 +1,3 @@ + + + diff --git a/scripts/build.ps1 b/scripts/build.ps1 index b45375c..a87404a 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -1,6 +1,9 @@ $path_root = split-path -Path $PSScriptRoot -Parent $path_build = join-path $path_root 'build' -$path_part_1 = join-path $path_root 'part_1' +$path_code = join-path $path_root 'code' +$path_duffle = join-path $path_code 'duffle' +$path_8086 = join-path $path_code '8086' +$path_part_1 = join-path $path_code 'part_1' $path_scripts = join-path $path_root 'scripts' $path_toolchain = join-path $path_root 'toolchain' @@ -13,6 +16,64 @@ if ((test-path $path_build) -eq $false) { $compiler = "clang" $linker = 'lld-link.exe' +# General Compiler Flags +$f_compile = "-c" +$f_debug = "-g" +$f_define = "-D" +$f_include = "-I" +$f_output = "-o" +$f_std_c11 = "-std=c11" +$f_std_c23 = "-std=c23" + +$f_exceptions_disabled = '-fno-exceptions' +$f_diagnostics_absolute_paths = '-fdiagnostics-absolute-paths' + +# Warning Flags +$f_wall = "-Wall" +$f_wno_attributes = "-Wno-attributes" + +# Optimization Flags +$f_optimize_none = "-O0" +$f_optimize_size = "-Os" +$f_optimize_intrinsics = "-Oi" +$f_optimize_debug = "-Og" +$f_omit_frame_ptr = "-fomit-frame-pointer" + +$f_path_output = '-o' +$f_wall = '-Wall' +$f_nologo = '/nologo' + +# Environment & Standard Library Flags +$f_no_stdlib = "-nostdlib" +$f_freestanding = "-ffreestanding" +$f_no_builtin = "-fno-builtin" + +# Linker-related Flags (for Compiler) +$f_code_sections = "-ffunction-sections" +$f_data_sections = "-fdata-sections" +$f_no_strict_alias = "-fno-strict-aliasing" + + +# Linker Flags (passed via -Wl,) + +$f_link_pass_through_prefix = "-Wl," +$f_link_mapfile = "-Map=" # Usage: $flag_link_pass_through_prefix + $flag_link_mapfile + path +$f_link_gc_sections = "--gc-sections" +$f_link_format = "--oformat=" +$f_link_start_group = "--start-group" +$f_link_end_group = "--end-group" +$f_link_static = "-static" +$f_link_script = "-T" +$f_link_lib_path = "-L" +$f_link_lib = "-l" + +$f_link_no_incremental = '/INCREMENTAL:NO' +$f_link_win_subsystem_console = '/SUBSYSTEM:CONSOLE' +$f_link_win_machine_64 = '/MACHINE:X64' +$f_link_win_debug = '/DEBUG' +$f_link_win_pdb = '/PDB:' +$f_link_win_path_output = '/OUT:' + function compile-unit { param( [string] $unit, [string] $link_module, @@ -20,27 +81,87 @@ function compile-unit { param( [string[]]$user_compile_args ) $compile_args = @() + $compile_args += $f_wall + $compile_args += $f_c11 + $compile_args += $f_no_optimization + $compile_args += $f_diagnostics_absolute_paths + $compile_args += $f_exceptions_disabled + # $compiler_args += "-nostdlib" + # $compiler_args += "-ffreestanding" + $compile_args += $user_compile_args + + $compile_args += $f_compile + $compile_args += $unit, ($f_output + $link_module) write-host "Compiling '$unit' -> '$link_module'" -ForegroundColor DarkCyan # $compile_args | ForEach-Object { Write-Host "`t$_" -ForegroundColor Green } - & $Compiler $compile_args + & $compiler $compile_args if ($LASTEXITCODE -ne 0) { write-error "Compilation failed for $unit. Aborting."; exit 1 } } -function link-modules { param([string[]]$link_modules, [string] $elf, [string[]]$user_link_args) +function link-modules { param([string[]]$link_modules, [string] $module, [string] $pdb, [string[]]$user_link_args) $link_args = @() + $link_args = @() + $link_args += $f_nologo + $link_args += $f_link_win_machine_64 + $link_args += $f_link_no_incremental + $link_args += ($f_link_win_path_output + $binary) + $link_args += $f_link_win_debug + $link_args += $f_link_win_pdb + $pdb + $link_args += $f_link_mapfile + $map + $link_args += $f_link_win_subsystem_console + # $link_args += "/nodefaultlib" + + $base_name = [System.IO.Path]::GetFileNameWithoutExtension($module) + $map = join-path $path_build "$base_name.map" + $link_args += ($f_link_pass_through_prefix + $f_link_mapfile + $map) + + $link_args += ($f_link_pass_through_prefix + $f_link_start_group) + $libraries = @( + 'kernel32.lib', + 'user32.lib', + 'gdi32.lib' + ) + foreach ($lib in $libraries) { + $link_args += ($f_link_lib + $lib) + } + + $link_args += $link_modules + + $final_link_args = @($link_args) + ($f_output + $elf) write-host "Linking modules into '$elf'" -ForegroundColor DarkCyan $final_link_args += ($f_link_pass_through_prefix + $f_link_end_group) # $final_link_args | foreach-object { write-host $_ } - & $Compiler $final_link_args - & mipsel-none-elf-objdump.exe -W $elf >> $dasm + & $linker $final_link_args if ($LASTEXITCODE -ne 0) { write-error "Linking failed. Aborting."; exit 1 } } -function build-part_1() { - -} -build-part_1() +function build-part_1 { + $includes = @( + $path_duffle, + $path_8086, + $path_part_1 + ) + $source_c = join-path $path_part_1 'sim_8086.c' + $module_c = join-path $path_build 'sim_8086.o' + + $compile_args = @() + $compile_args += ($flag_define + 'BUILD_DEBUG=1') + $compile_args += $f_debug + $compiler_args += $f_optimize_none + compile-unit $source_c $module_c $includes $compile_args + + $pdb = join-path $path_build 'sim_8086.pdb' + $exe = join-path $path_build 'sim_8086.exe' + + $link_args = @() + $link_args += $f_debug + $link_modules = @( + $module_c + ) + link-modules $link_modules $exe $pdb $link_args +} +build-part_1 diff --git a/scripts/helpers/misc.lua b/scripts/helpers/misc.lua deleted file mode 100644 index e69de29..0000000