overkill 32 bits avx2 assembly code
[mini2dgl.git] / fasm / source / macos / fasmg.asm
blobcf3a5404904846a6571631c357234a0a45914bc0
2 match ,{
4 err ; fasm 1 assembly not supported
6 } match -,{
7 else
9 include 'selfhost.inc'
11 end match
12 _ equ }
14 format MachO executable
15 entry start
17 include '../version.inc'
19 interpreter '/usr/lib/dyld'
20 uses '/usr/lib/libSystem.B.dylib'
22 import libc.malloc,'_malloc',\
23 libc.realloc,'_realloc',\
24 libc.free,'_free',\
25 libc.fopen,'_fopen',\
26 libc.fclose,'_fclose',\
27 libc.fread,'_fread',\
28 libc.fwrite,'_fwrite',\
29 libc.fseek,'_fseek',\
30 libc.ftell,'_ftell',\
31 libc.time,'_time',\
32 libc.write,'_write',\
33 getenv,'_getenv',\
34 gettimeofday,'_gettimeofday',\
35 exit,'_exit'
37 struct timeval
38 time_t dd ?
39 suseconds_t dd ?
40 ends
42 segment '__TEXT' readable executable
44 section '__text' align 16
46 start:
47 mov ecx,[esp]
48 mov [argc],ecx
49 lea ebx,[esp+4]
50 mov [argv],ebx
52 call system_init
54 call get_arguments
55 mov bl,al
56 cmp [no_logo],0
57 jne logo_ok
58 mov esi,_logo
59 xor ecx,ecx
60 call display_string
61 logo_ok:
62 test bl,bl
63 jnz display_usage_information
65 xor al,al
66 mov ecx,[verbosity_level]
67 jecxz init
68 or al,TRACE_ERROR_STACK
69 dec ecx
70 jz init
71 or al,TRACE_DISPLAY
72 init:
73 call assembly_init
75 ccall gettimeofday,start_time,0
77 assemble:
78 mov esi,[initial_commands]
79 mov edx,[source_path]
80 call assembly_pass
81 jc assembly_done
83 mov eax,[current_pass]
84 cmp eax,[maximum_number_of_passes]
85 jb assemble
87 call show_display_data
89 mov esi,_error_prefix
90 xor ecx,ecx
91 call display_error_string
92 mov esi,_code_cannot_be_generated
93 xor ecx,ecx
94 call display_error_string
95 mov esi,_message_suffix
96 xor ecx,ecx
97 call display_error_string
99 jmp assembly_failed
101 assembly_done:
103 call show_display_data
105 cmp [first_error],0
106 jne assembly_failed
108 cmp [no_logo],0
109 jne summary_done
110 mov eax,[current_pass]
111 xor edx,edx
112 call itoa
113 call display_string
114 mov esi,_passes
115 cmp [current_pass],1
116 jne display_passes_suffix
117 mov esi,_pass
118 display_passes_suffix:
119 xor ecx,ecx
120 call display_string
121 ccall gettimeofday,end_time,0
122 mov eax,[end_time.time_t]
123 sub eax,[start_time.time_t]
124 mov ecx,1000000
125 mul ecx
126 add eax,[end_time.suseconds_t]
127 adc edx,0
128 sub eax,[start_time.suseconds_t]
129 sbb edx,0
130 add eax,50000
131 mov ecx,1000000
132 div ecx
133 mov ebx,eax
134 mov eax,edx
135 xor edx,edx
136 mov ecx,100000
137 div ecx
138 mov [tenths_of_second],eax
139 xchg eax,ebx
140 or ebx,eax
141 jz display_output_length
142 xor edx,edx
143 call itoa
144 call display_string
145 mov esi,_message_suffix
146 mov ecx,1
147 call display_string
148 mov eax,[tenths_of_second]
149 xor edx,edx
150 call itoa
151 call display_string
152 mov esi,_seconds
153 xor ecx,ecx
154 call display_string
155 display_output_length:
156 call get_output_length
157 push eax edx
158 call itoa
159 call display_string
160 pop edx eax
161 mov esi,_bytes
162 cmp eax,1
163 jne display_bytes_suffix
164 test edx,edx
165 jnz display_bytes_suffix
166 mov esi,_byte
167 display_bytes_suffix:
168 xor ecx,ecx
169 call display_string
170 mov esi,_new_line
171 xor ecx,ecx
172 call display_string
173 summary_done:
175 mov ebx,[source_path]
176 mov edi,[output_path]
177 call write_output_file
178 jc write_failed
180 call assembly_shutdown
181 call system_shutdown
183 ccall exit,0
185 assembly_failed:
187 call show_errors
189 call assembly_shutdown
190 call system_shutdown
192 ccall exit,2
194 write_failed:
195 mov ebx,_write_failed
196 jmp fatal_error
198 out_of_memory:
199 mov ebx,_out_of_memory
200 jmp fatal_error
202 fatal_error:
204 mov esi,_error_prefix
205 xor ecx,ecx
206 call display_error_string
207 mov esi,ebx
208 xor ecx,ecx
209 call display_error_string
210 mov esi,_message_suffix
211 xor ecx,ecx
212 call display_error_string
214 call assembly_shutdown
215 call system_shutdown
217 ccall exit,3
219 display_usage_information:
221 mov esi,_usage
222 xor ecx,ecx
223 call display_string
225 call system_shutdown
227 ccall exit,1
229 get_arguments:
230 xor eax,eax
231 mov [initial_commands],eax
232 mov [source_path],eax
233 mov [output_path],eax
234 mov [no_logo],al
235 mov [verbosity_level],eax
236 mov [maximum_number_of_passes],100
237 mov [maximum_number_of_errors],1
238 mov [maximum_depth_of_stack],10000
239 mov ecx,[argc]
240 mov ebx,[argv]
241 add ebx,4
242 dec ecx
243 jz error_in_arguments
244 get_argument:
245 mov esi,[ebx]
246 mov al,[esi]
247 cmp al,'-'
248 je get_option
249 cmp [source_path],0
250 jne get_output_file
251 mov [source_path],esi
252 jmp next_argument
253 get_output_file:
254 cmp [output_path],0
255 jne error_in_arguments
256 mov [output_path],esi
257 jmp next_argument
258 get_option:
259 inc esi
260 lodsb
261 cmp al,'e'
262 je set_errors_limit
263 cmp al,'E'
264 je set_errors_limit
265 cmp al,'i'
266 je insert_initial_command
267 cmp al,'I'
268 je insert_initial_command
269 cmp al,'p'
270 je set_passes_limit
271 cmp al,'P'
272 je set_passes_limit
273 cmp al,'r'
274 je set_recursion_limit
275 cmp al,'R'
276 je set_recursion_limit
277 cmp al,'v'
278 je set_verbose_mode
279 cmp al,'V'
280 je set_verbose_mode
281 cmp al,'n'
282 je set_no_logo
283 cmp al,'N'
284 jne error_in_arguments
285 set_no_logo:
286 or [no_logo],-1
287 cmp byte [esi],0
288 je next_argument
289 error_in_arguments:
290 or al,-1
292 set_verbose_mode:
293 cmp byte [esi],0
294 jne get_verbose_setting
295 dec ecx
296 jz error_in_arguments
297 add ebx,4
298 mov esi,[ebx]
299 get_verbose_setting:
300 call get_option_value
301 cmp edx,2
302 ja error_in_arguments
303 mov [verbosity_level],edx
304 jmp next_argument
305 set_errors_limit:
306 cmp byte [esi],0
307 jne get_errors_setting
308 dec ecx
309 jz error_in_arguments
310 add ebx,4
311 mov esi,[ebx]
312 get_errors_setting:
313 call get_option_value
314 test edx,edx
315 jz error_in_arguments
316 mov [maximum_number_of_errors],edx
317 jmp next_argument
318 set_recursion_limit:
319 cmp byte [esi],0
320 jne get_recursion_setting
321 dec ecx
322 jz error_in_arguments
323 add ebx,4
324 mov esi,[ebx]
325 get_recursion_setting:
326 call get_option_value
327 test edx,edx
328 jz error_in_arguments
329 mov [maximum_depth_of_stack],edx
330 jmp next_argument
331 set_passes_limit:
332 cmp byte [esi],0
333 jne get_passes_setting
334 dec ecx
335 jz error_in_arguments
336 add ebx,4
337 mov esi,[ebx]
338 get_passes_setting:
339 call get_option_value
340 test edx,edx
341 jz error_in_arguments
342 mov [maximum_number_of_passes],edx
343 next_argument:
344 add ebx,4
345 dec ecx
346 jnz get_argument
347 cmp [source_path],0
348 je error_in_arguments
349 xor al,al
351 get_option_value:
352 xor eax,eax
353 mov edx,eax
354 find_option_value:
355 cmp byte [esi],20h
356 jne get_option_digit
357 inc esi
358 jmp find_option_value
359 get_option_digit:
360 lodsb
361 test al,al
362 jz option_value_ok
363 sub al,30h
364 jc invalid_option_value
365 cmp al,9
366 ja invalid_option_value
367 imul edx,10
368 jo invalid_option_value
369 add edx,eax
370 jc invalid_option_value
371 jmp get_option_digit
372 option_value_ok:
373 dec esi
376 invalid_option_value:
379 insert_initial_command:
380 cmp byte [esi],0
381 jne measure_initial_command
382 dec ecx
383 jz error_in_arguments
384 add ebx,4
385 mov esi,[ebx]
386 measure_initial_command:
387 push ebx ecx edi
388 mov edi,esi
389 or ecx,-1
390 xor al,al
391 repne scasb
392 not ecx
393 dec ecx
394 mov edi,[initial_commands]
395 lea eax,[ecx+2]
396 test edi,edi
397 jz allocate_initial_commands_buffer
398 mov edx,[initial_commands_length]
399 add edi,edx
400 add eax,edx
401 cmp eax,[initial_commands_maximum_length]
402 ja grow_initial_commands_buffer
403 copy_initial_command:
404 rep movsb
405 mov ax,0Ah
406 stosw
407 dec edi
408 sub edi,[initial_commands]
409 mov [initial_commands_length],edi
410 pop edi ecx ebx
411 jmp next_argument
412 allocate_initial_commands_buffer:
413 push ecx
414 mov ecx,eax
415 call malloc
416 mov [initial_commands],eax
417 mov [initial_commands_maximum_length],ecx
418 mov edi,eax
419 pop ecx
420 jmp copy_initial_command
421 grow_initial_commands_buffer:
422 push ecx
423 mov ecx,eax
424 mov eax,[initial_commands]
425 call realloc
426 mov [initial_commands],eax
427 mov [initial_commands_maximum_length],ecx
428 mov edi,eax
429 add edi,[initial_commands_length]
430 pop ecx
431 jmp copy_initial_command
433 include 'system.inc'
435 include '../assembler.inc'
436 include '../symbols.inc'
437 include '../expressions.inc'
438 include '../conditions.inc'
439 include '../floats.inc'
440 include '../directives.inc'
441 include '../calm.inc'
442 include '../errors.inc'
443 include '../map.inc'
444 include '../reader.inc'
445 include '../output.inc'
446 include '../console.inc'
448 section '__cstring' align 4
450 _logo db 'flat assembler version g.',VERSION,10,0
452 _usage db 'Usage: fasmg source [output]',10
453 db 'Optional settings:',10
454 db ' -p limit Set the maximum allowed number of passes (default 100)',10
455 db ' -e limit Set the maximum number of displayed errors (default 1)',10
456 db ' -r limit Set the maximum depth of stack (default 10000)',10
457 db ' -v flag Enable or disable showing all lines from the stack (default 0)',10
458 db ' -i command Insert instruction at the beginning of source',13,10
459 db ' -n Do not show logo nor summary',13,10
460 db 0
462 _pass db ' pass, ',0
463 _passes db ' passes, ',0
464 _dot db '.'
465 _seconds db ' seconds, ',0
466 _byte db ' byte.',0
467 _bytes db ' bytes.',0
469 _write_failed db 'failed to write the output file',0
470 _out_of_memory db 'not enough memory to complete the assembly',0
471 _code_cannot_be_generated db 'could not generate code within the allowed number of passes',0
473 _open_mode db 'r',0
474 _create_mode db 'w',0
476 include '../tables.inc'
477 include '../messages.inc'
479 segment '__DATA' readable writable
481 section '__bss' align 4
483 include '../variables.inc'
485 source_path dd ?
486 output_path dd ?
487 maximum_number_of_passes dd ?
489 initial_commands dd ?
490 initial_commands_length dd ?
491 initial_commands_maximum_length dd ?
493 argc dd ?
494 argv dd ?
495 timestamp dq ?
497 start_time timeval
498 end_time timeval
499 tenths_of_second dd ?
501 verbosity_level dd ?
502 no_logo db ?
504 path_buffer rb 1000h