123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- ;; This file is part of asmc, a bootstrapping OS with minimal seed
- ;; Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
- ;; https://gitlab.com/giomasce/asmc
- ;; This program is free software: you can redistribute it and/or modify
- ;; it under the terms of the GNU General Public License as published by
- ;; the Free Software Foundation, either version 3 of the License, or
- ;; (at your option) any later version.
- ;; This program is distributed in the hope that it will be useful,
- ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
- ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ;; GNU General Public License for more details.
- ;; You should have received a copy of the GNU General Public License
- ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
- str_platform_g_compile:
- db 'platform_g_compile'
- db 0
- str_platform_setjmp:
- db 'platform_setjmp'
- db 0
- str_platform_longjmp:
- db 'platform_longjmp'
- db 0
- str_init_compile_main:
- db 'Will now compile main.g...'
- db NEWLINE
- db 0
- str_init_launch_main:
- db 'Will now call main!'
- db NEWLINE
- db 0
- str_main_g:
- db 'main.g'
- db 0
- str_main:
- db 'main'
- db 0
- ;; void platform_g_compile(char *filename)
- platform_g_compile:
- ;; Prepare to write in memory
- mov eax, write_mem_ptr
- mov ecx, heap_ptr
- mov edx, [ecx]
- mov [eax], edx
- ;; Load the parameter and save some registers
- mov eax, [esp+4]
- push edx
- push edx
- ;; Open the file
- push eax
- call platform_open_file
- add esp, 4
- pop edx
- ;; Assemble the file
- push 0
- push edx
- push 0
- push eax
- call compile
- add esp, 16
- ;; Actually allocate used heap memory, so that new allocations will
- ;; not overwrite it
- mov eax, write_mem_ptr
- mov ecx, heap_ptr
- mov edx, [eax]
- sub edx, [ecx]
- push edx
- call platform_allocate
- add esp, 4
- ;; Assert that the allocation gave us what we expected
- pop edx
- cmp edx, eax
- jne platform_panic
- ;; Discard temporary labels
- call discard_temp_labels
- ret
- discard_temp_labels:
- push esi
- push edi
- push ebx
- ;; Use esi for the source index and edi for the dst index
- mov esi, 0
- mov edi, 0
- discard_temp_labels_loop:
- ;; Check for termination
- mov eax, symbol_num
- cmp [eax], esi
- je discard_temp_labels_end
- ;; Check if the symbol is a temp label
- mov ecx, symbol_names_ptr
- mov ecx, [ecx]
- mov eax, MAX_SYMBOL_NAME_LEN
- mul esi
- add eax, ecx
- cmp BYTE [eax], DOT
- je discard_temp_label_loop_end
- ;; If the two pointers are equal, do nothing
- cmp esi, edi
- je discard_temp_label_update_dest
- ;; Copy name
- mov ebx, eax
- mov eax, MAX_SYMBOL_NAME_LEN
- mul edi
- add eax, ecx
- push ebx
- push eax
- call strcpy
- add esp, 8
- ;; Copy location
- mov ecx, symbol_locs_ptr
- mov ecx, [ecx]
- mov eax, 4
- mul esi
- add eax, ecx
- mov ebx, eax
- mov eax, 4
- mul edi
- add eax, ecx
- mov edx, [ebx]
- mov [eax], edx
- ;; Copy arity
- mov ecx, symbol_arities_ptr
- mov ecx, [ecx]
- mov eax, 4
- mul esi
- add eax, ecx
- mov ebx, eax
- mov eax, 4
- mul edi
- add eax, ecx
- mov edx, [ebx]
- mov [eax], edx
- discard_temp_label_update_dest:
- add edi, 1
- discard_temp_label_loop_end:
- ;; Increment reading pointer and reloop
- add esi, 1
- jmp discard_temp_labels_loop
- discard_temp_labels_end:
- ;; Save the new length
- mov eax, symbol_num
- mov [eax], edi
- pop ebx
- pop edi
- pop esi
- ret
- start:
- ;; Init compiler
- call init_g_compiler
- call init_g_operations
- ;; Add some asmg-specific platform_* calls
- push 1
- push platform_g_compile
- push str_platform_g_compile
- call add_symbol
- add esp, 12
- push 1
- push platform_setjmp
- push str_platform_setjmp
- call add_symbol
- add esp, 12
- push 2
- push platform_longjmp
- push str_platform_longjmp
- call add_symbol
- add esp, 12
- ;; Log
- push str_init_compile_main
- push 1
- call platform_log
- add esp, 8
- ;; Assemble main.asm
- push str_main_g
- call platform_g_compile
- add esp, 4
- ;; Log
- push str_init_launch_main
- push 1
- call platform_log
- add esp, 8
- ;; Set EBP to zero, so that stack traces originating from the G code
- ;; are cut
- push ebp
- mov ebp, 0
- ;; Enable PMC
- call enable_pmc
- ;; Call main
- push 0
- push str_main
- call platform_get_symbol
- add esp, 8
- call eax
- pop ebp
- ret
- ret:
- ret
- str_equal:
- db '='
- db 0
- equal:
- mov eax, [esp+4]
- mov ecx, [esp+8]
- mov [ecx], eax
- ret
- str_equalc:
- db '=c'
- db 0
- equalc:
- mov eax, [esp+4]
- mov ecx, [esp+8]
- mov [ecx], al
- ret
- str_param:
- db 'param'
- db 0
- param:
- mov eax, [esp+4]
- mov edx, 4
- mul edx
- add eax, 8
- add eax, ebp
- mov eax, [eax]
- ret
- str_plus:
- db '+'
- db 0
- plus:
- mov eax, [esp+8]
- add eax, [esp+4]
- ret
- str_minus:
- db '-'
- db 0
- minus:
- mov eax, [esp+8]
- sub eax, [esp+4]
- ret
- str_un_minus:
- db '--'
- db 0
- un_minus:
- mov eax, [esp+8]
- neg eax
- ret
- str_times:
- db '*'
- db 0
- times_op:
- mov eax, [esp+8]
- imul DWORD [esp+4]
- ret
- str_over:
- db '/'
- db 0
- over:
- mov edx, 0
- mov eax, [esp+8]
- idiv DWORD [esp+4]
- ret
- str_mod:
- db '%'
- db 0
- mod:
- mov edx, 0
- mov eax, [esp+8]
- idiv DWORD [esp+4]
- mov eax, edx
- ret
- str_eq:
- db '=='
- db 0
- eq:
- mov eax, 1
- mov ecx, [esp+8]
- cmp ecx, [esp+4]
- je ret
- mov eax, 0
- ret
- str_neq:
- db '!='
- db 0
- neq:
- mov eax, 1
- mov ecx, [esp+8]
- cmp ecx, [esp+4]
- jne ret
- mov eax, 0
- ret
- str_l:
- db '<'
- db 0
- l:
- mov eax, 1
- mov ecx, [esp+8]
- cmp ecx, [esp+4]
- jl ret
- mov eax, 0
- ret
- str_le:
- db '<='
- db 0
- le:
- mov eax, 1
- mov ecx, [esp+8]
- cmp ecx, [esp+4]
- jle ret
- mov eax, 0
- ret
- str_g:
- db '>'
- db 0
- g:
- mov eax, 1
- mov ecx, [esp+8]
- cmp ecx, [esp+4]
- jg ret
- mov eax, 0
- ret
- str_ge:
- db '>='
- db 0
- ge:
- mov eax, 1
- mov ecx, [esp+8]
- cmp ecx, [esp+4]
- jge ret
- mov eax, 0
- ret
- str_shl:
- db '<<'
- db 0
- shl:
- mov ecx, [esp+4]
- mov eax, [esp+8]
- shl eax, cl
- ret
- str_shr:
- db '>>'
- db 0
- shr:
- mov ecx, [esp+4]
- mov eax, [esp+8]
- shr eax, cl
- ret
- str_and:
- db '&'
- db 0
- and:
- mov eax, [esp+8]
- and eax, [esp+4]
- ret
- str_or:
- db '|'
- db 0
- or:
- mov eax, [esp+8]
- or eax, [esp+4]
- ret
- str_xor:
- db '^'
- db 0
- xor:
- mov eax, [esp+8]
- xor eax, [esp+4]
- ret
- str_not:
- db '~'
- db 0
- not:
- mov eax, [esp+4]
- not eax
- ret
- str_land:
- db '&&'
- db 0
- land:
- mov eax, 0
- cmp DWORD [esp+8], 0
- je ret
- cmp DWORD [esp+4], 0
- je ret
- mov eax, 1
- ret
- str_lor:
- db '||'
- db 0
- lor:
- mov eax, 1
- cmp DWORD [esp+8], 0
- jne ret
- cmp DWORD [esp+4], 0
- jne ret
- mov eax, 0
- ret
- str_lnot:
- db '!'
- db 0
- lnot:
- mov eax, 0
- cmp DWORD [esp+4], 0
- jne ret
- mov eax, 1
- ret
- str_deref:
- db '**'
- db 0
- deref:
- mov eax, [esp+4]
- mov eax, [eax]
- ret
- str_derefc:
- db '**c'
- db 0
- derefc:
- mov edx, [esp+4]
- mov eax, 0
- mov al, [edx]
- ret
- str_inb:
- db 'inb'
- db 0
- inb:
- mov edx, [esp+4]
- mov eax, 0
- in al, dx
- ret
- str_inw:
- db 'inw'
- db 0
- inw:
- mov edx, [esp+4]
- mov eax, 0
- in ax, dx
- ret
- str_ind:
- db 'ind'
- db 0
- ind:
- mov edx, [esp+4]
- mov eax, 0
- in eax, dx
- ret
- str_outb:
- db 'outb'
- db 0
- outb:
- mov eax, [esp+4]
- mov edx, [esp+8]
- out dx, al
- ret
- str_outw:
- db 'outw'
- db 0
- outw:
- mov eax, [esp+4]
- mov edx, [esp+8]
- out dx, ax
- ret
- str_outd:
- db 'outd'
- db 0
- outd:
- mov eax, [esp+4]
- mov edx, [esp+8]
- out dx, eax
- ret
- str_itoa:
- db 'itoa'
- db 0
- str_frame_ptr:
- db '__frame_ptr'
- db 0
- frame_ptr:
- mov eax, ebp
- ret
- str_max_symbol_name_len:
- db '__max_symbol_name_len'
- db 0
- max_symbol_name_len:
- mov eax, MAX_SYMBOL_NAME_LEN
- ret
- str_symbol_table_len:
- db '__symbol_table_len'
- db 0
- symbol_table_len:
- mov eax, SYMBOL_TABLE_LEN
- ret
- str_symbol_num:
- db '__symbol_num'
- db 0
- symbol_num_func:
- mov eax, symbol_num
- mov eax, [eax]
- ret
- str_symbol_names:
- db '__symbol_names'
- db 0
- symbol_names_func:
- mov eax, symbol_names_ptr
- mov eax, [eax]
- ret
- str_symbol_locs:
- db '__symbol_locs'
- db 0
- symbol_locs_func:
- mov eax, symbol_locs_ptr
- mov eax, [eax]
- ret
- str_symbol_arities:
- db '__symbol_arities'
- db 0
- symbol_arities_func:
- mov eax, symbol_arities_ptr
- mov eax, [eax]
- ret
- str_ret_instr:
- db '__ret_instr'
- db 0
- ret_instr:
- call read_ret_instr
- ret
- init_g_operations:
- push 2
- push equal
- push str_equal
- call add_symbol
- add esp, 12
- push 2
- push equalc
- push str_equalc
- call add_symbol
- add esp, 12
- push 1
- push param
- push str_param
- call add_symbol
- add esp, 12
- push 2
- push plus
- push str_plus
- call add_symbol
- add esp, 12
- push 2
- push minus
- push str_minus
- call add_symbol
- add esp, 12
- push 1
- push un_minus
- push str_un_minus
- call add_symbol
- add esp, 12
- push 2
- push times_op
- push str_times
- call add_symbol
- add esp, 12
- push 2
- push over
- push str_over
- call add_symbol
- add esp, 12
- push 2
- push mod
- push str_mod
- call add_symbol
- add esp, 12
- push 2
- push eq
- push str_eq
- call add_symbol
- add esp, 12
- push 2
- push neq
- push str_neq
- call add_symbol
- add esp, 12
- push 2
- push l
- push str_l
- call add_symbol
- add esp, 12
- push 2
- push le
- push str_le
- call add_symbol
- add esp, 12
- push 2
- push g
- push str_g
- call add_symbol
- add esp, 12
- push 2
- push ge
- push str_ge
- call add_symbol
- add esp, 12
- push 2
- push shl
- push str_shl
- call add_symbol
- add esp, 12
- push 2
- push shr
- push str_shr
- call add_symbol
- add esp, 12
- push 2
- push and
- push str_and
- call add_symbol
- add esp, 12
- push 2
- push or
- push str_or
- call add_symbol
- add esp, 12
- push 2
- push xor
- push str_xor
- call add_symbol
- add esp, 12
- push 1
- push not
- push str_not
- call add_symbol
- add esp, 12
- push 2
- push land
- push str_land
- call add_symbol
- add esp, 12
- push 2
- push lor
- push str_lor
- call add_symbol
- add esp, 12
- push 1
- push lnot
- push str_lnot
- call add_symbol
- add esp, 12
- push 1
- push inb
- push str_inb
- call add_symbol
- add esp, 12
- push 1
- push inw
- push str_inw
- call add_symbol
- add esp, 12
- push 1
- push ind
- push str_ind
- call add_symbol
- add esp, 12
- push 2
- push outb
- push str_outb
- call add_symbol
- add esp, 12
- push 2
- push outw
- push str_outw
- call add_symbol
- add esp, 12
- push 2
- push outd
- push str_outd
- call add_symbol
- add esp, 12
- push 1
- push deref
- push str_deref
- call add_symbol
- add esp, 12
- push 1
- push derefc
- push str_derefc
- call add_symbol
- add esp, 12
- push 1
- push itoa
- push str_itoa
- call add_symbol
- add esp, 12
- push 0
- push frame_ptr
- push str_frame_ptr
- call add_symbol
- add esp, 12
- push 0
- push max_symbol_name_len
- push str_max_symbol_name_len
- call add_symbol
- add esp, 12
- push 0
- push symbol_table_len
- push str_symbol_table_len
- call add_symbol
- add esp, 12
- push 0
- push symbol_num_func
- push str_symbol_num
- call add_symbol
- add esp, 12
- push 0
- push symbol_names_func
- push str_symbol_names
- call add_symbol
- add esp, 12
- push 0
- push symbol_locs_func
- push str_symbol_locs
- call add_symbol
- add esp, 12
- push 0
- push symbol_arities_func
- push str_symbol_arities
- call add_symbol
- add esp, 12
- push 0
- push ret_instr
- push str_ret_instr
- call add_symbol
- add esp, 12
- ret
|