DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Back | Log | Files | Refs | Issues

cf.s (7753B)


      1 ;;;; PC32 COLORFORTH V 1.0 ;;;;
      2 bits 32
      3 
      4 
      5 ; VM SPEC
      6 ; 32bit VM
      7 ; calling C saves ebx, esi, edi, ebp
      8 ; data stack on esi, grows down
      9 ; eax TOS
     10 ; ecx, edx scratch (r1, r2)
     11 ; B on edi (because we store on B)
     12 ; A on ebx
     13 
     14 
     15 ; COMPILER SPEC
     16 
     17 
     18 %define LINK 0
     19 %define OLDLINK 0
     20 
     21 
     22 %macro CODE 2
     23 	%xdefine OLDLINK LINK
     24 	%xdefine LINK %2
     25 	%strlen len %1
     26 	align 4
     27 	dd OLDLINK
     28 	db %1
     29 	times (12-len) db 0
     30 	%2:
     31 %endmacro
     32 
     33 %macro OCALL 0		
     34 
     35 %endmacro
     36 
     37 %macro DUP 0			
     38 	sub esi, 4				; NUP make space for new data
     39 	mov [esi], eax
     40 %endmacro
     41 
     42 
     43 global comp
     44 global DATABLK
     45 global HEIGHT
     46 global WIDTH
     47 global CDATA
     48 
     49 extern cprint
     50 
     51 ; DATA BEGIN
     52 ; Windows (nasm -DWIN32): COFF sections can't be aligned past 64 bytes, so cf.s gets its own PE
     53 ; sections (the linker starts every section on a 4096-byte page) and alignments are padded
     54 ; relative to the section start.  The Linux build is unchanged.
     55 %ifdef WIN32
     56 %macro PAGEALIGN 0
     57 	times ((4096 - ($-$$)) & 4095) db 0
     58 %endmacro
     59 section .pc32d data read write execute
     60 align	64
     61 %else
     62 %macro PAGEALIGN 0
     63 	align 4096
     64 %endmacro
     65 section .data exec
     66 align	256
     67 %endif
     68 CSTK		dd 0x0						; stack position at COLD
     69 CEBP		dd 0x0
     70 HERE		dd 0x0						; compilation address
     71 L			dd 0x0						; last dict entry
     72 errorfn	dd error_dummy
     73 wbuf		times 32 db 0x0			; word buf
     74 WIDTH		dd 1024 ;1280					
     75 HEIGHT	dd 768; 1024
     76 CDATA		times 256 dd 0x0			; C&GL call data
     77 PAGEALIGN
     78 ; resb (one block of zeros) instead of incbin of empty files, and not "times N db 0" which is slow in nasm
     79 DATABLK	resb 0x2000000				; 32M memory
     80 PAGEALIGN
     81 PADM		resb 0x80000				; 512K
     82 VMEM		dd 0xCAFEBABE				; 8M video memory (marker word first, as in the old vmem.bin)
     83 			resb 0x800000-4
     84 SINE		incbin "data/sine.bin"
     85 FONT		incbin "data/fnt.bin"	; for ease of distribution
     86 
     87 dq 0
     88 
     89 ; CODE BEGIN
     90 %ifdef WIN32
     91 section .pc32t code read write execute
     92 %else
     93 section .text write
     94 %endif
     95 
     96 error_dummy:
     97 	ret
     98 
     99 CODE "O:O:", dofirst				; first word never gets searched since it points to 0
    100 	ret
    101 
    102 CODE "brk", dobreak				; for break points
    103 	ret 
    104 	ret
    105 
    106 CODE "L", dolatest
    107 	DUP
    108 	mov eax, [L]
    109 	ret
    110 	ret
    111 
    112 CODE "LINK", dolink
    113 	DUP
    114 	mov eax, L
    115 	ret
    116 	ret
    117 
    118 CODE "C#", docdata
    119 	DUP
    120 	mov eax, CDATA
    121 	ret
    122 	ret
    123 
    124 CODE "DATABLK", dodatablk
    125 	DUP 
    126 	mov eax, DATABLK
    127 	ret 
    128 	ret
    129 
    130 CODE "VMEM", dovmem
    131 	DUP
    132 	mov eax, VMEM
    133 	ret
    134 	ret
    135 
    136 CODE "ERRFN", doerrorfn
    137 	DUP
    138 	mov eax, errorfn
    139 	ret
    140 	ret
    141 
    142 CODE "SINT", dosinetable
    143 	DUP
    144 	mov eax, SINE
    145 	ret
    146 	ret
    147 
    148 CODE "FONT", dofntdata
    149 	DUP
    150 	mov eax, FONT
    151 	ret
    152 	ret
    153 
    154 CODE "isqrt", doinvsqrt		; copy pasted from godbolt, mulss goes faster directly from memory?
    155 	cvtsi2ss        xmm0, eax
    156 	mov     ecx, 1597463007
    157 	mulss   xmm0, dword [.LCPI0_0]
    158 	movd    eax, xmm0
    159 	mulss   xmm0, dword [.LCPI0_1]
    160 	sar     eax, 1
    161 	sub     ecx, eax
    162 	movd    xmm1, ecx
    163 	mulss   xmm0, xmm1
    164 	mulss   xmm0, xmm1
    165 	addss   xmm0, dword [.LCPI0_2]
    166 	mulss   xmm0, xmm1
    167 	mulss   xmm0, dword [.LCPI0_3]
    168 	cvttss2si       eax, xmm0
    169 	ret 
    170 	ret
    171 .LCPI0_0:	dd		964689920               ; float 2.44140625E-4
    172 .LCPI0_1:	dd		3204448256              ; float -0.5
    173 .LCPI0_2:	dd		1069547520              ; float 1.5
    174 .LCPI0_3:	dd		1166016512              ; float 4096
    175 
    176 
    177 CODE "simdtest", dosimdtest
    178 	movdqu xmm0, [eax]
    179 	lodsd 
    180 	movdqu xmm1, [eax]
    181 	paddd xmm0, xmm1
    182 	lodsd 
    183 	movups [eax], xmm0
    184 	ret 
    185 	ret
    186 
    187 
    188 ;; COMPILER ;;
    189 ; 1 byte tag to ease text
    190 ; 3 byte + 1 comment tag for cursor pos storage
    191 ; 1024 byte blocks but could be longer
    192 ; most compiler words can be called inside forth
    193 ; edi is used to hold HERE for compilation
    194 ; ebx pushed and holds SRC since we never use the A&B in compilation
    195 ; colors:
    196 ;	COMP				IMM			LEN/FLG
    197 ;	0	DEF		8	,JMP			12
    198 ;	1	CALL		9	IMM CALL		12
    199 ;	2	NUM		A	IMM NUM		HEX | FL | LEN 2b (no NEG)
    200 ;	3	REF		B	IMM REF		16
    201 ;	4	DATA		C	IMM DATA		16 (blue changes display)
    202 ;	5	ASM		D	MACRO			HEX | BASE5 | LEN 2b (keeps compat with SHBA)
    203 ;	6	EDIT		E 	COMMENT		same as text
    204 ;	7	---		F	---			
    205 
    206 %macro TAL 0
    207 	mov ecx, [ebx]
    208 	and ecx, 0xF0
    209 	shr ecx, 4
    210 %endmacro
    211 
    212 getlen:
    213 	TAL
    214 	cmp cl, 2
    215 	je .n
    216 	cmp cl, 5
    217 	je .n
    218 	cmp cl, 10
    219 	je .n
    220 	mov cl, [ebx]
    221 	and ecx, 0x0F
    222 	jmp .done
    223 .n:
    224 	mov cl, [ebx]
    225 	and ecx, 3
    226 	cmp cl, 3
    227 	je .add1
    228 	jmp .done
    229 .add1:	add cl, 1
    230 .done:
    231 	ret
    232 
    233 %macro TAG 0
    234 	mov cl, [ebx]
    235 	and ecx, 0xF0
    236 	shr ecx, 4
    237 %endmacro
    238 
    239 %macro NEXTW 0
    240 	call getlen
    241 	add ebx, ecx
    242 	inc ebx
    243 	call cword
    244 %endmacro
    245 
    246 
    247 zerobuf:	
    248 	push eax
    249 	push edi
    250 	mov eax, 0
    251 	mov edi, wbuf			; zero wbuf
    252 	stosd
    253 	stosd
    254 	stosd
    255 	stosd
    256 	pop edi
    257 	pop eax
    258 	ret
    259 
    260 cword:
    261 	call zerobuf
    262 	push esi
    263 	push edi
    264 	mov esi, ebx			; src
    265 	inc esi
    266 	mov edi, wbuf
    267 	call getlen
    268 	rep movsb
    269 	pop edi
    270 	pop esi
    271 	ret
    272 
    273 ; answer in ecx
    274 find:							; word already in wbuf
    275 	push esi
    276 	push edi
    277 	mov esi, [L]
    278 .search:
    279 	sub esi, 16				; 1st word links to nothing
    280 	cmp dword [esi], 0
    281 	je .n
    282 	push esi
    283 	add esi, 4				; name loc
    284 	mov edi, wbuf			; init search
    285 	mov ecx, 3
    286 	rep cmpsd
    287 	pop esi
    288 	jz .found
    289 	mov esi, [esi]			; next word
    290 	jmp .search
    291 .found:
    292 	add esi, 16
    293 	mov ecx, esi
    294 	jmp .e
    295 .n mov ecx, 0
    296 	pop edi
    297 	pop esi
    298 	jmp cferror
    299 .e pop edi
    300 	pop esi
    301 	ret
    302 
    303 CODE "ERROR", cferror
    304 	mov ecx, [errorfn]
    305 	call ecx
    306 	; jmp cmanage
    307 cmanage:
    308 	DUP
    309 	mov ecx, wbuf
    310 	push ecx
    311 	call cprint
    312 	mov ecx, HERE
    313 	mov [ecx], edi		; store new HERE
    314 	mov eax, esi
    315 	pop edi
    316 	pop ebx
    317 	pop ecx				; 0xDEADBABE
    318 	ret
    319 
    320 cftag:
    321 	push eax
    322 	push esi
    323 	mov eax, 0x10EB		; 16 byte jump
    324 	stosw
    325 	mov eax, [L]
    326 	stosd						; pointer to LATEST
    327 	mov esi, wbuf
    328 	movsd
    329 	movsd
    330 	movsd
    331 	mov eax, L
    332 	mov [eax], edi			; update latest
    333 	pop esi
    334 	pop eax
    335 	ret
    336 
    337 cfjmp:						
    338 	call find				; ecx
    339 	mov byte [edi], 0xE9	; jmp rel32
    340 storecall:
    341 	inc edi					
    342 	sub ecx, edi			; distance
    343 	sub ecx, 4				; instruction offset
    344 	mov [edi], ecx
    345 	add edi, 4
    346 	ret
    347 	
    348 cfcall: 	
    349 	call find
    350 	mov byte [edi], 0xE8 ; call rel32
    351 	jmp storecall
    352 
    353 
    354 immcall:
    355 	call find
    356 	jmp ecx
    357 	ret
    358 
    359 num1: xor ecx, ecx
    360 	mov cl, byte [ebx+1]
    361 	ret
    362 num2: xor ecx, ecx
    363 	mov cx, word [ebx+1]
    364 	ret
    365 num4: mov ecx, [ebx+1]
    366 	ret
    367 
    368 numtbl: dd cferror, num1, num2, num4, num4
    369 getnum:
    370 	call getlen
    371 	shl ecx, 2
    372 	add ecx, numtbl
    373 	jmp [ecx]
    374 
    375 cfnum:
    376 	call getnum					; ecx
    377 storeLIT:
    378 	push eax
    379 	mov eax, 0x8904EE83		; DUP & B8(load next word into eax)
    380 	stosd
    381 	mov eax, 0xB806
    382 	stosw
    383 	mov eax, ecx
    384 	stosd
    385 	pop eax
    386 	ret 
    387 
    388 immnum:
    389 	call getnum
    390 	DUP
    391 	mov eax, ecx
    392 	ret
    393 
    394 cfref:
    395 	call find
    396 	jmp storeLIT
    397 immref:
    398 	call find
    399 	DUP
    400 	mov eax, ecx
    401 	ret
    402 
    403 cfdata:
    404 	mov ecx, ebx
    405 	inc ecx
    406 	jmp storeLIT
    407 immdata:
    408 	DUP
    409 	mov eax, ebx
    410 	inc eax
    411 	ret
    412 	
    413 iasm1: mov byte [edi], cl
    414 	inc edi
    415 	ret
    416 iasm2: mov word [edi], cx
    417 	add edi, 2
    418 	ret
    419 iasm4: mov dword [edi], ecx
    420 	add edi, 4
    421 	ret
    422 
    423 asmtbl: dd cferror, iasm1, iasm2, iasm4, iasm4
    424 immasm:
    425 	call getlen
    426 	shl ecx, 2
    427 	mov edx, asmtbl
    428 	add edx, ecx
    429 	call getnum
    430 	jmp [edx]
    431 
    432 
    433 CODE "macro", domacro
    434 	push eax
    435 	mov ecx, eax
    436 	jmp cfmacro.loop
    437 cfmacro:
    438 	call find			; ecx
    439 	push eax
    440 .loop:
    441 	mov eax, [ecx]
    442 	and eax, 0xFFFF
    443 	cmp eax, 0xC3C3
    444 	je .done
    445 	stosb
    446 	inc ecx
    447 	jmp .loop
    448 .done:
    449 	pop eax
    450 
    451 cfcomment: ret
    452 
    453 align 32
    454 jmptbl:
    455 	dd cftag, cfcall, cfnum, cfref, cfdata, immasm, cfcomment, cfcomment
    456 	dd cfjmp, immcall, immnum, immref, immdata, cfmacro, cfcomment, cfcomment
    457 
    458 CODE "eval", doeval	
    459 	call getlen
    460 	cmp ecx, 0
    461 	jz .done
    462 	TAG
    463 	shl ecx, 2
    464 	mov edx, jmptbl
    465 	add edx, ecx
    466 	call [edx]
    467 	NEXTW
    468 	jmp doeval
    469 .done:
    470 	ret
    471 	ret
    472 
    473 
    474 CODE "bye", dobye
    475 	DUP
    476 	mov eax, esi
    477 	mov ebp, [CEBP]
    478 	mov esp, [CSTK]
    479 	ret
    480 
    481 CODE "COLD", doreset
    482 	mov ebp, [CEBP]
    483 	mov esp, [CSTK]
    484 comp:						; compiler setup
    485 	mov edx, L
    486 	mov dword [edx], LINK	; set latest properly
    487 	mov edx, CSTK
    488 	mov [edx], esp		; store stack for debugging
    489 	mov edx, CEBP
    490 	mov [edx], ebp		; to avoid segfaulting
    491 	mov esi, esp		
    492 	add esi, 512		; data stack
    493 	mov ebx, HERE
    494 	mov [ebx], dword DATABLK+((2880*1024)+1024)	; store reset HERE
    495 	mov ebx, 0	
    496 	mov edx, 0
    497 
    498 	mov eax, 0xCAFEBABE
    499 	push eax
    500 	mov eax, 0xDEADBABE
    501 	
    502 	mov ebx, DATABLK+2048
    503 	mov edi, [HERE]
    504 	call doeval
    505 .done:	
    506 	pop ecx
    507 	DUP
    508 	mov eax, esi
    509 	ret
    510 
    511 START:
    512 
    513 testesi:
    514