forked from lkundrak/dev86
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.86
562 lines (480 loc) · 6.16 KB
/
rules.86
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# Rules for optimizing BCC assembler output
# Rules for loading short variables
mov %2,%[ax|bx|cx|dx]1
mov %[ax|bx|cx|dx]1,%2
=
mov %2,%1
# Assign a constant
mov %[ax|bx|cx|dx]3,%[#|*]0%1
mov %2[%4],%[ax|bx|cx|dx]3
!BCC_EOS
=
mov word ptr %2[%4],%0%1
!BCC_EOS
# Assign a constant char
mov al,%[#|*]0%1
mov %2,al
!BCC_EOS
=
mov byte ptr %2,%0%1
!BCC_EOS
# Assign a constant long from a small int
xor ax,ax
mov bx,%[#|*]0%1
mov [%2],ax
mov [%2+2],bx
!BCC_EOS
=
mov word ptr [%2],#0
mov word ptr [%2+2],%0%1
!BCC_EOS
# Assign a constant long from a 2^16 mod int.
mov ax,%[#|*]0%1
xor bx,bx
mov [%2],ax
mov [%2+2],bx
!BCC_EOS
=
mov word ptr [%2],%0%1
mov word ptr [%2+2],#0
!BCC_EOS
# Assign a constant long from a big int
mov ax,%[#|*]4%1
mov bx,%[#|*]5%3
mov [%2],ax
mov [%2+2],bx
!BCC_EOS
=
mov word ptr [%2],%4%1
mov word ptr [%2+2],%5%3
!BCC_EOS
# rule for (char*) *x++
mov bx,%1
%[inc|dec]2 bx
mov %1,bx
mov al,-1[bx]
xor ah,ah
=
mov bx,%1
%2 word ptr %1
mov al,[bx]
xor ah,ah
# Rules for incrementing short variables
mov %[ax|bx|si|di]2,%1
%[inc|dec]3 %[ax|bx|si|di]2
mov %1,%[ax|bx|si|di]2
!BCC_EOS
=
%3 word ptr %1
!BCC_EOS
mov %[ax|bx|si|di]2,%1
%[inc|dec]3 %[ax|bx|si|di]2
mov %1,%[ax|bx|si|di]2
=
%3 word ptr %1
mov %2,%1
# Rules for manipulating characters with register char*s
inc %[si|di]*
inc %[si|di]*
mov al,-1[si]
mov -1[di],al
!BCC_EOS
=
lodsb
stosb
!BCC_EOS
inc %[si|di]*
inc %[si|di]*
mov al,-1[di]
mov -1[si],al
!BCC_EOS
=
xchg si,di
lodsb
stosb
xchg si,di
!BCC_EOS
inc si
mov al,-1[si]
=
lodsb
inc di
mov -1[di],al
=
stosb
dec %[si|di]*
dec %[si|di]*
mov al,1[si]
mov 1[di],al
=
std
lodsb
stosb
cld
dec %[si|di]*
dec %[si|di]*
mov al,1[di]
mov 1[si],al
=
std
xchg si,di
lodsb
stosb
xchg si,di
cld
dec si
mov al,1[si]
=
std
lodsb
cld
dec di
mov 1[di],al
=
std
stosb
cld
# Rules for manipulating short values
# These rules only apply at end of statment.
mov %[ax|bx]2,%1
%[add|and|xor|sub|or]4 %[ax|bx]2,%3
mov %1,%[ax|bx]2
!BCC_EOS
=
mov %2,%3
%4 %1,%2
!BCC_EOS
mov %[ax|bx]2,%1
%[add|and|xor|or]4 %[ax|bx]2,%3
mov %3,%[ax|bx]2
!BCC_EOS
=
mov %2,%1
%4 %3,%2
!BCC_EOS
mov al,%1
xor ah,ah
%[add|and|xor|sub|or]2 ax,%[#|*]0%3
mov %1,al
!BCC_EOS
=
%2 byte ptr %1,%0%3
!BCC_EOS
# This is safe with an embedded assign.
mov %[ax|bx]4,%1
%[add|and|xor|sub|or]2 %[si|di]3,%[ax|bx]4
=
%2 %3,%1
# Rules for comparing short values
# Compare doesn't need to leave any register in a specific state.
mov %[ax|bx]3,%1[%4]
cmp %[ax|bx]3,%[#|*]0%2
=
cmp word ptr %1[%4],%0%2
mov al,%1[%4]
cmp al,%[#|*]0%2
=
cmp byte ptr %1[%4],%0%2
mov %[ax|bx]3,%[ax|bx|cx|dx|si|di]2
cmp %[ax|bx]3,%1
=
cmp %2,%1
# General comparison rules
xor %1,%1
mov %2,%3
cmp %2,%1
=
mov %2,%3
cmp %2,#0
mov %1,%[#|*]0%2
mov %3,%4
cmp %3,%1
=
mov %3,%4
cmp %3,%0%2
mov %1,%2
cmp %1,%[#|*]00
%[jne |je ]4 %3
=
mov %1,%2
test %1,%1
%4 %3
%[and|xor|or]1 %2,%3
test %2,%2
=
%1 %2,%3
# General incrementation and decrementation rules
inc %3 ptr %1
mov %2,%1
dec %2
=
mov %2,%1
inc %3 ptr %1
dec %3 ptr %1
mov %2,%1
inc %2
=
mov %2,%1
dec %3 ptr %1
mov %1,%2
inc %3 ptr %2
push %1
=
push %3 ptr %2
inc %3 ptr %2
mov %1,%2
dec %3 ptr %2
push %1
=
push %3 ptr %2
dec %3 ptr %2
# Misc. rules
push word %[#|*]0%1
inc %[si|di]2
inc %[si|di]2
mov %[ax|bx|cx|dx]3,%*[bp]
mov %4[%[si|di]2],%[ax|bx|cx|dx]3
inc sp
inc sp
=
mov word ptr %(%4+2)[%2],%0%1
inc %2
inc %2
push %1
mov %[ax|bx|cx|dx]3,%2
%[add|and|xor|or]4 %[ax|bx|cx|dx]3,%*[bp]
inc sp
inc sp
=
mov %3,%1
%4 %3,%2
# If %1 is ax this fails badly.
# push %1
# mov %[ax|bx|cx|dx]3,%2
# %[add|sub|and|xor|or]5 %[ax|bx|cx|dx]3,%6
# %[add|sub|and|xor|or]4 %[ax|dx|cx|dx]3,%*[bp]
# inc sp
# inc sp
# =
# mov %3,%2
# %5 %3,%6
# %4 %3,%1
push %1
mov %[ax|bx|cx|dx]3,%*[bp]
%[add|sub|and|xor|or]4 %2,%[ax|bx|cx|dx]3
inc sp
inc sp
=
mov %3,%1
%4 %2,%3
mov %1,%[ax|bx|cx|dx]2
push %1
=
mov %1,%2
push %2
# Rules to remove superflous mov instructions
mov %1,%2
mov %1,%2
=
mov %1,%2
mov %1,%2
.%3:
mov %1,%2
=
.%3:
mov %1,%2
mov %1,%2
mov %2,%1
=
mov %1,%2
mov %1,%[ax|bx|cx|dx|eax|ebx|ecx|edx]2
mov %[ax|bx|cx|dx|eax|ebx|ecx|edx]3,%1
=
mov %1,%2
mov %3,%2
mov %1,%1
=
# poof
# Sometimes the compiler puts a jump right after a ret or another jump
# (in if-else-if constructions). Eliminate the second unnecessary jump.
# Also it puts a jump to the next instruction in extensive if-else
# constructions. That jump can also be removed.
jmp .%1
jmp .%2
=
jmp .%1
ret
jmp %2
=
ret
jmp .%1
.%1:
=
.%1:
jmp .%1
.%2:
!BCC_EOS
.%1:
=
.%2:
!BCC_EOS
.%1:
jmp .%1
.%3:
.%2:
.%1:
=
.%3:
.%2:
.%1:
# When using register variables the compiler initializes them when
# they are found in the source code, and sets up the stack frame for
# other local variables lateron. Move initialization of register variables
# behind the add-sp instruction to make further optimization easier.
proc_start
mov %[si|di]3,%1
add sp,*%2
=
proc_start
add sp,*%2
mov %3,%1
proc_start
mov %[si|di]3,%1
dec sp
dec sp
=
proc_start
dec sp
dec sp
mov %3,%1
add sp,*%1
add sp,*%2
=
add sp,*%1+%2
dec sp
dec sp
add sp,*%1
=
add sp,*%1-2
add sp,*%1
dec sp
dec sp
=
add sp,*%1-2
inc sp
inc sp
%1 %2
inc sp
inc sp
=
%1 %2
add sp,*4
inc sp
inc sp
%1 %2
add sp,*%3
=
%1 %2
add sp,*%3+2
add sp,*%3
%1 %2
inc sp
inc sp
=
%1 %2
add sp,*%3+2
add sp,*%3
%1 %2
add sp,*%4
=
%1 %2
add sp,*%3+%4
# And a great bug floating push/pop
push dx
push cx
push bx
push ax
pop ax
pop bx
pop cx
pop dx
=
# poof
# Long right shift by 16 unsigned
mov ax,%1
mov bx,%2
xchg bx,ax
xor bx,bx
=
mov ax,%2
xor bx,bx
# Long right shift by 16 signed
mov ax,%1
mov bx,%2
xchg bx,ax
cwd
=
mov ax,%2
cwd
# Load long and exchange
mov ax,%1
mov bx,%2
xchg bx,ax
=
mov bx,%1
mov ax,%2
# Repeated clear.
xor bx,bx
xchg bx,ax
xor ax,ax
=
xchg bx,ax
xor ax,ax
# Long expression assigned to an int
cwd
mov bx,dx
mov %1,ax
!BCC_EOS
=
mov %1,ax
!BCC_EOS
# Load unsigned into high word of long
mov ax,%1
xchg bx,ax
xor ax,ax
=
mov bx,%1
xor ax,ax
# Souped up strcpy; "while(*d++ = *s++);" in a function with no reg vars.
.%3:
!BCC_EOS
.%4:
mov bx,%1
inc bx
mov %1,bx
mov si,%2
inc si
mov %2,si
mov al,-1[bx]
mov -1[si],al
test al,al
jne .%3
=
.%4:
mov si,%1
mov di,%2
.%3:
lodsb
stosb
test al,al
jne .%3
mov %1,si
mov %2,di
!BCC_EOS
###
mov ax,%1
mov %[ax|bx|cx|dx]2,ax
mov ax,%3
=
mov %2,%1
mov ax,%3