Skip to content

Latest commit

ย 

History

History
211 lines (156 loc) ยท 7.65 KB

File metadata and controls

211 lines (156 loc) ยท 7.65 KB

RFC-018 โ€” hexa native compiler ์ง์ ‘ codegen spec

  • ์ƒํƒœ: Spec draft (2026-05-09) โ€” ๋ฏธ๊ตฌํ˜„
  • ์ž‘์„ฑ์ผ: 2026-05-09
  • ์„ ํ–‰ RFC: RFC-017 (atlas ์ •์  ๋‚ด์žฅ + native compiler ์ „ํ™˜ ๊ฒฐ์ •)
  • ๊ฒฐ์ • (2026-05-09): backend ํ›„๋ณด (LLVM / C-transpile / ์ง์ ‘ codegen) ์ค‘ ์ง์ ‘ codegen ์ฑ„ํƒ
  • ์˜ํ–ฅ ์˜์—ญ: ์‹ ์„ค compiler/ ํŠธ๋ฆฌ, stdlib/diagnostics/*, stdlib/rt/*, ๊ธฐ์กด hexa_interp ๋Š” stage0 ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ์šฉ๋„๋กœ๋งŒ ์ž”์กด

1. ๋™๊ธฐ (Why ์ง์ ‘ codegen)

ํ›„๋ณด ์žฅ์  ์•ฝ์  ์ฑ„ํƒ?
LLVM ์ตœ์ ํ™” ๊ฐ•๋ ฅ, ๋ฉ€ํ‹ฐํƒ€๊ฒŸ ๊ฑฐ๋Œ€ ์˜์กด (์ˆ˜๋ฐฑMB), ๋นŒ๋“œ ์‹œ๊ฐ„ โ†‘, atlas-aware hook ์–ด๋ ค์›€, AI-native ์ œ์–ด ์•ฝํ•จ โŒ
C-transpile ๋น ๋ฅธ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ (clang๋งŒ ์žˆ์œผ๋ฉด) C ์ปดํŒŒ์ผ๋Ÿฌ ์˜์กด ์‚ฌ์Šฌ, ๋””๋ฒ„๊ทธ ๋งคํ•‘ ์†์‹ค, atlas ์ž„๋ฒ ๋”ฉ ์–ด์ƒ‰, ๋ณด์•ˆ surface โ†‘ โŒ
์ง์ ‘ codegen ์ œ์–ด๊ถŒ 100%, ์˜์กด zero, atlas/diagnostic์„ IR ๋ชจ๋“  ๋‹จ๊ณ„์—์„œ ์ธ์ง€, ๊ฒฐ์ •์  ์ดˆ๊ธฐ ๋…ธ๋ ฅ ๅคง, ์ตœ์ ํ™” ์ง์ ‘ ์ž‘์„ฑ ํ•„์š” โœ…

ํ•ต์‹ฌ ์ด์œ : hexa-lang์€ atlas SSOT + ์ˆ˜์‹ ๊ฒ€์ฆ + AI-native ๊ฐ€ ๋ณธ์งˆ. ์™ธ๋ถ€ toolchain์€ ์ด ์ธ์ง€๋ฅผ IR ๊ฒฝ๊ณ„์—์„œ ์žƒ์Œ. ์ง์ ‘ codegen์€ ๋ชจ๋“  ๋‹จ๊ณ„๊ฐ€ atlas-aware.


2. ์ปดํŒŒ์ผ ํŒŒ์ดํ”„๋ผ์ธ

.hexa source
   โ†“ lex                      (tokens)
   โ†“ parse                    (AST, atlas-tagged)
   โ†“ resolve   (S0โ€“S2)        atlas ๋…ธ๋“œ / scope binding
   โ†“ check     (S3โ€“S5, S8)    type / units / citation
   โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ†‘ ์œ„๋ฐ˜ ์‹œ abort, ๋ฐ”์ด๋„ˆ๋ฆฌ ์•ˆ ๋งŒ๋“ฆ
   โ†“ lower                    HIR (typed)
   โ†“ mono                     generic monomorphization
   โ†“ ssa                      MIR (CFG, SSA)
   โ†“ optimize                 const-fold, dce, inline (๋ณด์ˆ˜์ )
   โ†“ regalloc                 LIR (target-specific)
   โ†“ emit                     asm text or machine code
   โ†“ link                     ELF (Linux) / Mach-O (macOS)

๊ฐ ๋‹จ๊ณ„๋Š” hexa๋กœ ์ž‘์„ฑ๋œ ๋ชจ๋“ˆ. ๋ชจ๋“  IR ๋…ธ๋“œ๋Š” atlas-aware (P/C/L ์ธ์šฉ ์ •๋ณด๋ฅผ IR ๋๊นŒ์ง€ ๋ณด์กด).


3. ํƒ€๊ฒŸ ๋งคํŠธ๋ฆญ์Šค

Tier Triple ์šฐ์„ ์ˆœ์œ„ ๋น„๊ณ 
T0 x86_64-linux-gnu 1์ฐจ ๋Œ€๋ถ€๋ถ„ dev / CI
T1 arm64-apple-darwin 1์ฐจ Apple Silicon dev
T2 arm64-linux-gnu 2์ฐจ docker/runners (build/hexa_interp.linux ํ›„์†)
T3 wasm32-unknown-unknown ํ›„์ˆœ์œ„ playground ์šฉ
T4 riscv64-linux-gnu ๋ณด๋„ˆ์Šค ๊ฒ€์ฆ์šฉ

ํฌ๋กœ์Šค ์ปดํŒŒ์ผ์€ stage1 ์ •์ฐฉ ํ›„ ๋„์ž….


4. ABI / calling convention

ํƒ€๊ฒŸ ๊ทœ์•ฝ
x86_64 Linux System V AMD64 (rdi, rsi, rdx, rcx, r8, r9, ...)
arm64 macOS AAPCS64 (x0โ€“x7, ...)
arm64 Linux AAPCS64 (๋™์ผ)

๊ทœ์น™

  • struct โ‰ค 16B by value, ๊ทธ ์ด์ƒ by reference
  • ๋ฐ˜ํ™˜๊ฐ’ โ‰ค 16B by register, ๊ทธ ์ด์ƒ hidden first arg
  • extern "C" fn ์€ ์œ„ ABI ๋”ฐ๋ฆ„
  • extern "hexa" fn (๋””ํดํŠธ)๋„ ๊ฐ™์€ ABI๋กœ ์‹œ์ž‘ โ€” ์ถ”ํ›„ hexa-๋‚ด๋ถ€ ์ตœ์ ํ™” (closure ์บก์ฒ˜ ๋“ฑ) ๊ฐ€๋Šฅ์„ฑ ์—ด์–ด๋‘ 

5. ๋ฉ”๋ชจ๋ฆฌ / runtime (hexa_rt)

์ตœ์†Œ runtime, ์ •์  ๋งํฌ.

๋ชจ๋“ˆ ์ฑ…์ž„ ๋น„๊ณ 
rt/alloc mmap ๊ธฐ๋ฐ˜ arena + bump 1.x: gc ์—†์Œ. 2.x: refcount (Swift/Rust Arc ๋ชจ๋ธ) ๊ฒ€ํ† 
rt/syscall direct syscall (Linux x86_64), libSystem (macOS) libc ์˜์กด 0 (์„ ํƒ ์‚ฌํ•ญ)
rt/panic stderr ๋ฉ”์‹œ์ง€ + exit no unwinding ์ดˆ๊ธฐ
rt/atlas ์ •์  atlas ์ธ๋ฑ์Šค (binary embed) hash ํ•€ ๊ฒ€์ฆ
rt/diag runtime panic ๋ฉ”์‹œ์ง€ (RFC-019 catalog ์ผ๋ถ€ ํฌํ•จ) binary embed
rt/io print, read, file ์ตœ์†Œ

ํฌ๊ธฐ ๋ชฉํ‘œ: stripped runtime โ‰ค 200 KB. atlas ์ •์  ์ž„๋ฒ ๋“œ ํฌํ•จ ์‹œ 2 MB ์ดํ•˜.


6. atlas ์ž„๋ฒ ๋”ฉ hook (RFC-017 ยง 4.5 ๊ตฌํ˜„)

๋นŒ๋“œ ํƒ€์ž„

์ปดํŒŒ์ผ๋Ÿฌ ๋นŒ๋“œ ์‹œ (one-time per compiler version):
  1. ~/core/nexus/n6/atlas.n6 + atlas.append.*.n6 ๋จธ์ง€
  2. dedup, schema check, P/C/L hash ๊ณ„์‚ฐ
  3. packed binary form ์ƒ์„ฑ (~1โ€“2 MB)
  4. ์ปดํŒŒ์ผ๋Ÿฌ ๋ฐ”์ด๋„ˆ๋ฆฌ .rodata ์„น์…˜์— ์ •์  ์ž„๋ฒ ๋“œ
  5. atlas SHA256 = ATLAS_HASH ์ƒ์ˆ˜

์‚ฌ์šฉ์ž ๋นŒ๋“œ ์‹œ

hexa build x.hexa
  โ†’ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์•ˆ atlas ์ธ๋ฑ์Šค๋กœ ์ฆ‰์‹œ P[*]/C[*]/L[*] ๊ฒ€์ฆ
  โ†’ ์‚ฌ์šฉ์ž ์ฝ”๋“œ๊ฐ€ ์ธ์šฉํ•œ atlas ๋…ธ๋“œ๋“ค๋งŒ dead-code elim ํ›„
     ์‚ฌ์šฉ์ž ๋ฐ”์ด๋„ˆ๋ฆฌ์— ์ž„๋ฒ ๋“œ (reflect / `hexa explain` ์šฉ)
  โ†’ atlas ๋ฏธ์‚ฌ์šฉ ์‹œ ์‚ฌ์šฉ์ž ๋ฐ”์ด๋„ˆ๋ฆฌ์— atlas 0 byte ํฌํ•จ

drift ๊ฐ์ง€

  • hexa.toml [atlas] hash = "a3f9..." ๋ช…์‹œ ์‹œ: ์ปดํŒŒ์ผ๋Ÿฌ atlas hash์™€ mismatch โ†’ ๋นŒ๋“œ ์‹คํŒจ
  • ๋ฏธ๋ช…์‹œ ์‹œ: ์ปดํŒŒ์ผ๋Ÿฌ์— ๋ฐ•ํžŒ hash ์‚ฌ์šฉ

override (์™ธ๋ถ€ ์‚ฌ์šฉ์ž)

  • hexa.toml [atlas] path = "..." โ†’ ์ปดํŒŒ์ผ๋Ÿฌ ๋นŒ๋“œ ์‹œ ๋‹ค๋ฅธ atlas ๋จธ์ง€
  • ๋‹จ, ์ปดํŒŒ์ผ๋Ÿฌ ์ž์ฒด ์žฌ๋นŒ๋“œ ํ•„์š”

7. bootstrap ์‹œํ€€์Šค

Stage ์‚ฐ์ถœ๋ฌผ ๋นŒ๋“œ ๋„๊ตฌ
Stage 0 hexa๋กœ ์ž‘์„ฑ๋œ minimal native compiler ์†Œ์Šค ๊ธฐ์กด hexa_interp (์ธํ„ฐํ”„๋ฆฌํ„ฐ) ๊ฐ€ ๋นŒ๋“œ
Stage 1 stage0๊ฐ€ ์ธํ„ฐํ”„๋ฆฌํŠธํ•ด์„œ emitํ•œ native ์ปดํŒŒ์ผ๋Ÿฌ ๋ฐ”์ด๋„ˆ๋ฆฌ stage0
Stage 2 stage1 ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๊ธฐ ์ž์‹  ์†Œ์Šค๋ฅผ native ์ปดํŒŒ์ผ stage1
Stage 3 stage2 ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๊ธฐ ์ž์‹  โ€” self-hosted ์ •์ฐฉ stage2

๊ฒ€์ฆ

  • bytewise(stage2_binary, stage3_binary) == match โ†’ fixed point ๋„๋‹ฌ
  • ์ผ์น˜ ์‹œ stage1, stage2 ํ๊ธฐ ๊ฐ€๋Šฅ

ํšŒ๊ท€

  • bench/bootstrap_e2e.hexa โ€” ๋งค PR์—์„œ stage0โ†’3 ์ „์ฒด ์žฌํ˜„
  • atlas hash drift, ์ปดํŒŒ์ผ๋Ÿฌ hash drift ๋ชจ๋‘ ํšŒ๊ท€ detect

8. ๋””๋ฒ„๊ทธ ์ •๋ณด

ํƒ€๊ฒŸ ํ˜•์‹
Linux DWARF v4 (๋ผ์ธ ๋งคํ•‘ + ๋ณ€์ˆ˜ + types)
macOS DWARF in __DWARF segment + dsym ์˜ต์…˜

์ถ”๊ฐ€ hexa-specific ์„น์…˜

  • .hexa.atlas โ€” ์‚ฌ์šฉ๋œ atlas ๋…ธ๋“œ hash ๋ฆฌ์ŠคํŠธ
  • .hexa.diag โ€” ์ปดํŒŒ์ผ ์‹œ emit๋œ hint ๋ฉ”์‹œ์ง€ (์‹คํ–‰ ์‹œ panic์— ๋™๋ด‰)
  • .hexa.spec โ€” @law/@implements ์–ด๋…ธํ…Œ์ด์…˜ ๋ฉ”ํƒ€

โ†’ hexa explain <binary> ๊ฐ€ binary ์ž์ฒด์—์„œ ์ •๋ณด ์ถ”์ถœ.


9. ์ตœ์ ํ™” 1์ฐจ ๋ฒ”์œ„ (๋ณด์ˆ˜์ )

๋‹จ๊ณ„ ๋„์ž…
const folding 1์ฐจ
dead code elimination 1์ฐจ
inlining 1์ฐจ (์ž‘์€ ํ•จ์ˆ˜๋งŒ)
common subexpression elim 2์ฐจ
loop invariant motion 2์ฐจ
SROA (struct decompose) 2์ฐจ
auto-vectorization ํ›„์ˆœ์œ„
LTO ํ›„์ˆœ์œ„
PGO ํ›„์ˆœ์œ„

๋ชฉํ‘œ: 1์ฐจ hexa ์ปดํŒŒ์ผ๋Ÿฌ๋Š” "์ •ํ™•ํ•˜๊ณ  ์ž‘๊ณ  ๋””๋ฒ„๊น… ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ ์ƒ์„ฑ" ์šฐ์„ . -O2 ์ˆ˜์ค€์€ stage3 ์ •์ฐฉ ํ›„.


10. linker

๋‹จ๊ณ„ ์ „๋žต
1์ฐจ system ld / lld ํ˜ธ์ถœ (๊ฐ€์žฅ ๋น ๋ฅธ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ)
2์ฐจ ์ž์ฒด ์ •์  ๋ง์ปค hexa_ld โ€” ELF/Mach-O minimal emitter
3์ฐจ ์ž์ฒด ๋™์  ๋ง์ปค๋„ ๊ฒ€ํ†  (hexa runtime ์—†๋Š” ํ™˜๊ฒฝ์šฉ)

ํ•˜์ด๋ธŒ๋ฆฌ๋“œ: hexa build --linker=system|hexa ํ”Œ๋ž˜๊ทธ.


11. FFI

  • extern "C" fn: System V/AAPCS ๊ทธ๋Œ€๋กœ
  • extern "py" fn (RFC-016 import py): native ์ปดํŒŒ์ผ๋Ÿฌ + Python embedded โ€” libpython3.so ๋™์  ๋งํฌ ๋˜๋Š” subprocess
  • atlas ๋…ธ๋“œ ์ธ์šฉ์€ FFI ๊ฒฝ๊ณ„์—์„œ ๋ณด์กด (debug section ํ†ตํ•ด)

12. ๋ฏธํ•ด๊ฒฐ / ํ›„์†

  1. inline asm ๋ฌธ๋ฒ• (Rust asm! ๋งคํฌ๋กœ ๋ชจ๋ธ? hexa-native?)
  2. generic monomorphization vs dyn dispatch ๋””ํดํŠธ
  3. async runtime (tokio ๋ชจ๋ธ vs go-routine ๋ชจ๋ธ)
  4. lifetime / borrow check (Rust strict vs gc-light)
  5. effect system (atlas L[*] ์™€ ๊ฒฐ์†ํ•ด effect๋ฅผ law๋กœ?)
  6. linker ์ž์ฒด ์ž‘์„ฑ ์‹œ์ 
  7. wasm32 backend ์šฐ์„ ์ˆœ์œ„
  8. ์ž์ฒด ์–ด์…ˆ๋ธ”๋Ÿฌ vs as ์™ธ๋ถ€ ํ˜ธ์ถœ

13. ํ•œ ์ค„ ๊ฒฐ๋ก 

์ง์ ‘ codegen โ€” AST โ†’ HIR โ†’ MIR โ†’ LIR โ†’ mach 5๋‹จ IR, 1์ฐจ ํƒ€๊ฒŸ x86_64-linux + arm64-darwin, runtime โ‰ค2MB (atlas ํฌํ•จ), bootstrap 4-stage (interp โ†’ self-host). ๋ชจ๋“  IR ๋‹จ๊ณ„๊ฐ€ atlas-aware, ์™ธ๋ถ€ toolchain ์˜์กด zero.