← 목록
기타 2026-06-21 4KB 읽기 5분

네이티브 코드젠 ARM/RISC-V 완료 (2026-06-21)

결과

한선씨 → TOAU → toau2c → C → arm-none-eabi-gcc → qemu-system-arm 실행 = 58 == 인터프리터 58 riscv64-elf-gcc → qemu-system-riscv64 실행 = 58 == 인터프리터 58

검증

인터프리터:  ./hanseonc_high arm_제곱데모.한선 > demo.toau && ./crownyc run demo.toau  → 58
ARM 네이티브: arm-none-eabi-gcc (freestanding, nostdlib) → qemu -M mps2-an385 → 58
RISC-V:     riscv64-elf-gcc (medany) → qemu -M virt -bios none → 58 (stderr)

변경 파일

  • toau2c.c: 생성 C에서 #include <stdio.h> 제거, PRINT(324)/STR_PRINT(345) 인라인화 (rt_putint/rt_puts)
  • runtime_stub.h: rt_puts/rt_putint/rt_str_valid/rt_str_ptr/rt_str_len 선언 추가
  • runtime_stub.c: 호스트판 rt_puts/rt_putint (write syscall), rt_str_* 래퍼 추가
  • runtime_stub_arm.c [신설]: ARM 베어메탈 런타임 — 세미호스팅(BKPT 0xAB) + 정적 스택/힙 (ARM_STACK_CAP=256, MEM_CAP=1024)
  • runtime_stub_riscv.c [신설]: RISC-V 64 런타임 — 세미호스팅(ebreak+magic) + 정적 스택/힙
  • startup_cm3.c [신설]: Cortex-M3 벡터테이블 + Reset_Handler + semi_exit
  • startup_riscv.c [신설]: RISC-V naked _start (la sp,_stack_top) + _c_start
  • crowny_cm3.ld [신설]: mps2-an385 링커스크립트 (FLASH 0x0/256K, RAM 0x20000000/256K)
  • crowny_riscv.ld [신설]: virt 링커스크립트 (TEXT 0x80000000/2M, DATA 0x80200000/2M)
  • arm_제곱데모.한선 [신설]: 검증용 데모 소스
  • runtime_stub_arm.한선 [신설]: 한선씨 동반

빌드 명령

bash# ARM
./hanseonc_high arm_제곱데모.한선 > /tmp/demo.toau 2>/dev/null
./toau2c /tmp/demo.toau /tmp/demo.c
arm-none-eabi-gcc -ffreestanding -nostdlib -O2 -mcpu=cortex-m3 -mthumb -I. \
  /tmp/demo.c runtime_stub_arm.c startup_cm3.c -T crowny_cm3.ld -o /tmp/demo.elf
qemu-system-arm -M mps2-an385 -cpu cortex-m3 -semihosting \
  -semihosting-config enable=on,target=native -kernel /tmp/demo.elf -nographic -serial /dev/null

# RISC-V (출력은 stderr)
riscv64-elf-gcc -ffreestanding -nostdlib -O2 -march=rv64imac_zicsr -mabi=lp64 \
  -mcmodel=medany -I. /tmp/demo.c runtime_stub_riscv.c startup_riscv.c \
  -T crowny_riscv.ld -o /tmp/rv_demo.elf
qemu-system-riscv64 -M virt -bios none -semihosting \
  -semihosting-config enable=on,target=native -kernel /tmp/rv_demo.elf -nographic 2>&1 | grep 58

함정 (다음 세션 필독)

  • RISC-V 세미호스팅 출력은 stderr2>&1 없으면 stdout에 안 나옴
  • RISC-V naked _start에 sp 초기화 필수 — C 함수는 스택 없으면 세그폴트
  • -mcmodel=medany 필수 — medlow는 0x80000000 주소에서 reloc 오류
  • runtime_stub_arm.c는 crownyc.c 미포함 — 정적 VM (malloc 불필요)
  • ARM 세미호스팅 SYS_EXIT: block[2]={0x20026, code}, BKPT 0xAB
  • toau2c 생성 C stdio.h 없음 — printf/fwrite 없이 rt_putint/rt_puts 사용

WASM 추가 완료 (2026-06-21)

emscripten 5.0.7 설치 + WASM 경로 검증:

node /tmp/demo_wasm.js → 58 == 인터프리터 58 ✅

4중 일치 최종 확인

실행 경로출력
인터프리터 (./crownyc run)58
호스트 네이티브 (cc -O2)58
ARM Cortex-M3 (qemu mps2-an385)58
RISC-V 64 (qemu virt)58
WASM (node demo.js)58

신설 파일

  • runtime_stub_wasm.c: emscripten 판 런타임 (printf 출력, 정적 VM)
  • runtime_stub_wasm.한선: 한선씨 동반

WASM 빌드 명령

bash./hanseonc_high arm_제곱데모.한선 > /tmp/demo.toau 2>/dev/null
./toau2c /tmp/demo.toau /tmp/demo.c
emcc -O2 -I. /tmp/demo.c runtime_stub_wasm.c -o /tmp/demo.js
node /tmp/demo.js   # → 58