std: 표준 라이브러리가 컴파일러 옆에서 해석되고 호출된다

std 는 예약된 이름이고 프로그램이 아니라 컴파일러와 함께 있으므로 자기 루트를
갖는다 (--std=). 본문 없는 선언은 링커가 찾을 것 -- 런타임이나 C 라이브러리 --
이므로 IR 에 extern 으로 나간다.

런타임에 write/alloc/free/exit 를 넣었다. 이것이 표준 라이브러리가 스스로
말할 수 없는 전부이고 나머지는 Ferro 로 쓴다.

@trap @unreachable @size_of @align_of @line 을 내린다.

링크 이름에서 점과 괄호를 걸렀다. 유닛 경로에는 점이 있고 제네릭 인스턴스에는
괄호가 있는데 어셈블러가 받지 않는다.
This commit is contained in:
2026-08-17 06:12:44 +09:00
parent 174e6c569d
commit 4624c6d0ec
9 changed files with 202 additions and 12 deletions
+79
View File
@@ -122,6 +122,85 @@ reason_write:
call _ExitProcess@4
fe_trap endp
; ---------------------------------------------------------------- primitives
; The standard library is written in Ferro; these are the few things it cannot
; say for itself. All cdecl.
extern _GetProcessHeap@0 : near
extern _HeapAlloc@12 : near
extern _HeapFree@12 : near
; fe_rt_write(handle, ptr, len) -> bytes written
public fe_rt_write
fe_rt_write proc near
push ebp
mov ebp, esp
push ebx
mov eax, [ebp+8] ; 1 = stdout, 2 = stderr
cmp eax, 2
je pick_err
push -11
jmp pick_done
pick_err:
push -12
pick_done:
call _GetStdHandle@4
push 0
push offset written
push dword ptr [ebp+16]
push dword ptr [ebp+12]
push eax
call _WriteFile@20
mov eax, [written]
pop ebx
mov esp, ebp
pop ebp
ret
fe_rt_write endp
; fe_rt_alloc(n) -> pointer, or zero
public fe_rt_alloc
fe_rt_alloc proc near
push ebp
mov ebp, esp
call _GetProcessHeap@0
push dword ptr [ebp+8]
push 8 ; HEAP_ZERO_MEMORY
push eax
call _HeapAlloc@12
mov esp, ebp
pop ebp
ret
fe_rt_alloc endp
; fe_rt_free(p)
public fe_rt_free
fe_rt_free proc near
push ebp
mov ebp, esp
mov eax, [ebp+8]
test eax, eax
je free_done
call _GetProcessHeap@0
push dword ptr [ebp+8]
push 0
push eax
call _HeapFree@12
free_done:
mov esp, ebp
pop ebp
ret
fe_rt_free endp
; fe_rt_exit(code) -- never returns
public fe_rt_exit
fe_rt_exit proc near
push ebp
mov ebp, esp
push dword ptr [ebp+8]
call _ExitProcess@4
fe_rt_exit endp
public fe_start_
fe_start_ proc near
call fe_main_