FreeBASIC  0.91.0
emit.bi
Go to the documentation of this file.
1 #ifndef __EMIT_BI__
2 #define __EMIT_BI__
3 
4 #include once "flist.bi"
5 #include once "reg.bi"
6 #include once "ast.bi"
7 #include once "ir.bi"
8 
9 const EMIT_INITNODES = 2048
11 
12 '' TODO: x86 specific
13 const EMIT_REGCLASSES = 2 '' assuming FB_DATACLASS_ will start at 0!
14 
15 '' if changed, update the _opFnTB() arrays at emit_*.bas
16 enum EMIT_NODEOP
18 
19  '' load
23 
24  '' store
28 
29  '' bop
43 
46 
49 
50  '' rel
57 
58  '' uop
61 
63 
66 
70 
72 
83 
88 
89  '' branch
96 
97  '' misc
102 
103  '' mem
108 
109  '' dgb
114 
116 end enum
117 
130 end enum
131 
132 type EMIT_BOPNODE
133  op as integer
134  dvreg as IRVREG ptr
135  svreg as IRVREG ptr
136 end type
137 
138 type EMIT_UOPNODE
139  op as integer
140  dvreg as IRVREG ptr
141 end type
142 
143 type EMIT_RELNODE
144  op as integer
145  rvreg as IRVREG ptr
146  label as FBSYMBOL ptr
147  dvreg as IRVREG ptr
148  svreg as IRVREG ptr
149 end type
150 
151 type EMIT_STKNODE
152  op as integer
153  vreg as IRVREG ptr
154  extra as integer
155 end type
156 
157 type EMIT_BRCNODE
158  op as integer
159  vreg as IRVREG ptr
160  sym as FBSYMBOL ptr
161  extra as integer
162 end type
163 
164 type EMIT_SOPNODE
165  op as integer
166  sym as FBSYMBOL ptr
167 end type
168 
169 type EMIT_LITNODE
170  isasm as integer
171  text as zstring ptr
172 end type
173 
174 type EMIT_JTBNODE
175  tbsym as FBSYMBOL ptr
176 
177  '' Dynamically allocated buffer holding the jmptb's value/label pairs
178  values as ulongint ptr
179  labels as FBSYMBOL ptr ptr
180  labelcount as integer
181 
182  deflabel as FBSYMBOL ptr
183  minval as ulongint
184  maxval as ulongint
185 end type
186 
187 type EMIT_MEMNODE
188  op as integer
189  dvreg as IRVREG ptr
190  svreg as IRVREG ptr
191  bytes as integer
192  extra as integer
193 end type
194 
195 type EMIT_DBGNODE
196  op as integer
197  sym as FBSYMBOL ptr
198  lnum as integer
199  pos as integer
200 end type
201 
202 type EMIT_NODE
203  class as EMIT_NODECLASS_ENUM
204 
205  union
206  bop as EMIT_BOPNODE
207  uop as EMIT_UOPNODE
208  rel as EMIT_RELNODE
209  stk as EMIT_STKNODE
210  brc as EMIT_BRCNODE
211  sop as EMIT_SOPNODE
212  lit as EMIT_LITNODE
213  jtb as EMIT_JTBNODE
214  mem as EMIT_MEMNODE
215  dbg as EMIT_DBGNODE
216  end union
217 
219 end type
220 
221 
222 type EMIT_BOPCB as sub( byval dvreg as IRVREG ptr, _
223  byval svreg as IRVREG ptr )
224 
225 type EMIT_UOPCB as sub( byval dvreg as IRVREG ptr )
226 
227 type EMIT_RELCB as sub( byval rvreg as IRVREG ptr, _
228  byval label as FBSYMBOL ptr, _
229  byval dvreg as IRVREG ptr, _
230  byval svreg as IRVREG ptr )
231 
232 type EMIT_STKCB as sub( byval vreg as IRVREG ptr, _
233  byval extra as integer )
234 
235 type EMIT_BRCCB as sub( byval vreg as IRVREG ptr, _
236  byval sym as FBSYMBOL ptr, _
237  byval extra as integer )
238 
239 type EMIT_SOPCB as sub( byval sym as FBSYMBOL ptr )
240 
241 type EMIT_LITCB as sub( byval text as zstring ptr )
242 
243 type EMIT_JTBCB as sub _
244  ( _
245  byval tbsym as FBSYMBOL ptr, _
246  byval values1 as ulongint ptr, _
247  byval labels1 as FBSYMBOL ptr ptr, _
248  byval labelcount as integer, _
249  byval deflabel as FBSYMBOL ptr, _
250  byval minval as ulongint, _
251  byval maxval as ulongint _
252  )
253 
254 type EMIT_MEMCB as sub( byval dvreg as IRVREG ptr, _
255  byval svreg as IRVREG ptr, _
256  byval bytes as integer, _
257  byval extra as integer )
258 
259 type EMIT_DBGCB as sub( byval sym as FBSYMBOL ptr, _
260  byval lnum as integer, _
261  byval pos as integer )
262 
263 '' if changed, update the _vtbl symbols at emit_*.bas::*_ctor
264 type EMIT_VTBL
265  init as function _
266  ( _
267  ) as integer
268 
269  end as sub _
270  ( _
271  )
272 
273  getOptionValue as function _
274  ( _
275  byval opt as IR_OPTIONVALUE _
276  ) as integer
277 
278  open as function _
279  ( _
280  ) as integer
281 
282  close as sub _
283  ( _
284  byval tottime as double _
285  )
286 
287  isKeyword as function _
288  ( _
289  byval text as zstring ptr _
290  ) as integer
291 
292  isRegPreserved as function _
293  ( _
294  byval dclass as integer, _
295  byval reg as integer _
296  ) as integer
297 
298  getFreePreservedReg as function _
299  ( _
300  byval dclass as integer, _
301  byval dtype as integer _
302  ) as integer
303 
304  getResultReg as sub _
305  ( _
306  byval dtype as integer, _
307  byval dclass as integer, _
308  byref r1 as integer, _
309  byref r2 as integer _
310  )
311 
312  procGetFrameRegName as function _
313  ( _
314  ) as const zstring ptr
315 
316  procBegin as sub _
317  ( _
318  byval proc as FBSYMBOL ptr _
319  )
320 
321  procEnd as sub _
322  ( _
323  byval proc as FBSYMBOL ptr _
324  )
325 
326  procHeader as sub _
327  ( _
328  byval proc as FBSYMBOL ptr, _
329  byval initlabel as FBSYMBOL ptr _
330  )
331 
332  procFooter as sub _
333  ( _
334  byval proc as FBSYMBOL ptr, _
335  byval bytestopop as integer, _
336  byval initlabel as FBSYMBOL ptr, _
337  byval exitlabel as FBSYMBOL ptr _
338  )
339 
340  procAllocArg as sub _
341  ( _
342  byval proc as FBSYMBOL ptr, _
343  byval sym as FBSYMBOL ptr _
344  )
345 
346  procAllocLocal as sub _
347  ( _
348  byval proc as FBSYMBOL ptr, _
349  byval sym as FBSYMBOL ptr _
350  )
351 
352  procAllocStaticVars as sub(byval head_sym as FBSYMBOL ptr)
353 
354  scopeBegin as sub _
355  ( _
356  byval s as FBSYMBOL ptr _
357  )
358 
359  scopeEnd as sub _
360  ( _
361  byval s as FBSYMBOL ptr _
362  )
363 
364  setSection as sub _
365  ( _
366  byval section as integer, _
367  byval priority as integer _
368  )
369 
370  getTypeString as function _
371  ( _
372  byval dtype as integer _
373  ) as const zstring ptr
374 
375  getSectionString as function _
376  ( _
377  byval section as integer, _
378  byval priority as integer _
379  ) as const zstring ptr
380 end type
381 
382 type EMITCTX
383  inited as integer
384 
385  pos as integer '' to help debugging
386 
387  regTB(0 to EMIT_REGCLASSES-1) as REGCLASS ptr '' reg classes
388 
389  '' node tb
390  nodeTB as TFLIST
391  vregTB as TFLIST
392  curnode as EMIT_NODE ptr
393 
394  regUsedTB(EMIT_REGCLASSES-1) as REG_FREETB '' keep track of register usage
395 
396  '' platform-dependent
397  lastsection as integer
398  lastpriority as integer
399 
400  keyinited as integer
401  keyhash as THASH
402 
403  ''
404  vtbl as EMIT_VTBL
405  opFnTb as any ptr ptr
406 end type
407 
408 ''
409 ''
410 ''
411 declare function emitInit( ) as integer
412 declare sub emitEnd( )
413 
414 #if __FB_DEBUG__
415 declare function emitDumpRegName( byval dtype as integer, byval reg as integer ) as string
416 #endif
417 
418 declare function emitGetRegClass _
419  ( _
420  byval dclass as integer _
421  ) as REGCLASS ptr
422 
423 declare sub emitCOMMENT( byval text as zstring ptr )
424 declare sub emitASM( byval text as zstring ptr )
425 
426 declare function emitJMPTB _
427  ( _
428  byval tbsym as FBSYMBOL ptr, _
429  byval values1 as ulongint ptr, _
430  byval labels1 as FBSYMBOL ptr ptr, _
431  byval labelcount as integer, _
432  byval deflabel as FBSYMBOL ptr, _
433  byval minval as ulongint, _
434  byval maxval as ulongint _
435  ) as EMIT_NODE ptr
436 
437 declare function emitCALL _
438  ( _
439  byval label as FBSYMBOL ptr, _
440  byval bytestopop as integer _
441  ) as EMIT_NODE ptr
442 
443 declare function emitCALLPTR _
444  ( _
445  byval svreg as IRVREG ptr, _
446  byval bytestopop as integer _
447  ) as EMIT_NODE ptr
448 
449 declare function emitJUMP _
450  ( _
451  byval label as FBSYMBOL ptr _
452  ) as EMIT_NODE ptr
453 
454 declare function emitJUMPPTR _
455  ( _
456  byval svreg as IRVREG ptr _
457  ) as EMIT_NODE ptr
458 
459 declare function emitLABEL _
460  ( _
461  byval label as FBSYMBOL ptr _
462  ) as EMIT_NODE ptr
463 
464 declare function emitRET( byval bytestopop as integer ) as EMIT_NODE ptr
465 
466 declare function emitPUBLIC _
467  ( _
468  byval label as FBSYMBOL ptr _
469  ) as EMIT_NODE ptr
470 
471 declare function emitBRANCH _
472  ( _
473  byval op as integer, _
474  byval label as FBSYMBOL ptr _
475  ) as EMIT_NODE ptr
476 
477 declare function emitXchgTOS _
478  ( _
479  byval svreg as IRVREG ptr _
480  ) as EMIT_NODE ptr
481 
482 declare function emitMOV _
483  ( _
484  byval dvreg as IRVREG ptr, _
485  byval svreg as IRVREG ptr _
486  ) as EMIT_NODE ptr
487 
488 declare function emitSTORE _
489  ( _
490  byval dvreg as IRVREG ptr, _
491  byval svreg as IRVREG ptr _
492  ) as EMIT_NODE ptr
493 
494 declare function emitLOAD _
495  ( _
496  byval dvreg as IRVREG ptr, _
497  byval svreg as IRVREG ptr _
498  ) as EMIT_NODE ptr
499 
500 declare function emitADD _
501  ( _
502  byval dvreg as IRVREG ptr, _
503  byval svreg as IRVREG ptr _
504  ) as EMIT_NODE ptr
505 
506 declare function emitSUB _
507  ( _
508  byval dvreg as IRVREG ptr, _
509  byval svreg as IRVREG ptr _
510  ) as EMIT_NODE ptr
511 
512 declare function emitMUL _
513  ( _
514  byval dvreg as IRVREG ptr, _
515  byval svreg as IRVREG ptr _
516  ) as EMIT_NODE ptr
517 
518 declare function emitDIV _
519  ( _
520  byval dvreg as IRVREG ptr, _
521  byval svreg as IRVREG ptr _
522  ) as EMIT_NODE ptr
523 
524 declare function emitINTDIV _
525  ( _
526  byval dvreg as IRVREG ptr, _
527  byval svreg as IRVREG ptr _
528  ) as EMIT_NODE ptr
529 
530 declare function emitMOD _
531  ( _
532  byval dvreg as IRVREG ptr, _
533  byval svreg as IRVREG ptr _
534  ) as EMIT_NODE ptr
535 
536 declare function emitSHL _
537  ( _
538  byval dvreg as IRVREG ptr, _
539  byval svreg as IRVREG ptr _
540  ) as EMIT_NODE ptr
541 
542 declare function emitSHR _
543  ( _
544  byval dvreg as IRVREG ptr, _
545  byval svreg as IRVREG ptr _
546  ) as EMIT_NODE ptr
547 
548 declare function emitAND _
549  ( _
550  byval dvreg as IRVREG ptr, _
551  byval svreg as IRVREG ptr _
552  ) as EMIT_NODE ptr
553 
554 declare function emitOR _
555  ( _
556  byval dvreg as IRVREG ptr, _
557  byval svreg as IRVREG ptr _
558  ) as EMIT_NODE ptr
559 
560 declare function emitXOR _
561  ( _
562  byval dvreg as IRVREG ptr, _
563  byval svreg as IRVREG ptr _
564  ) as EMIT_NODE ptr
565 
566 declare function emitEQV _
567  ( _
568  byval dvreg as IRVREG ptr, _
569  byval svreg as IRVREG ptr _
570  ) as EMIT_NODE ptr
571 
572 declare function emitIMP _
573  ( _
574  byval dvreg as IRVREG ptr, _
575  byval svreg as IRVREG ptr _
576  ) as EMIT_NODE ptr
577 
578 declare function emitADDROF _
579  ( _
580  byval dvreg as IRVREG ptr, _
581  byval svreg as IRVREG ptr _
582  ) as EMIT_NODE ptr
583 
584 declare function emitDEREF _
585  ( _
586  byval dvreg as IRVREG ptr, _
587  byval svreg as IRVREG ptr _
588  ) as EMIT_NODE ptr
589 
590 declare function emitGT _
591  ( _
592  byval rvreg as IRVREG ptr, _
593  byval label as FBSYMBOL ptr, _
594  byval dvreg as IRVREG ptr, _
595  byval svreg as IRVREG ptr _
596  ) as EMIT_NODE ptr
597 
598 declare function emitLT _
599  ( _
600  byval rvreg as IRVREG ptr, _
601  byval label as FBSYMBOL ptr, _
602  byval dvreg as IRVREG ptr, _
603  byval svreg as IRVREG ptr _
604  ) as EMIT_NODE ptr
605 
606 declare function emitEQ _
607  ( _
608  byval rvreg as IRVREG ptr, _
609  byval label as FBSYMBOL ptr, _
610  byval dvreg as IRVREG ptr, _
611  byval svreg as IRVREG ptr _
612  ) as EMIT_NODE ptr
613 
614 declare function emitNE _
615  ( _
616  byval rvreg as IRVREG ptr, _
617  byval label as FBSYMBOL ptr, _
618  byval dvreg as IRVREG ptr, _
619  byval svreg as IRVREG ptr _
620  ) as EMIT_NODE ptr
621 
622 declare function emitLE _
623  ( _
624  byval rvreg as IRVREG ptr, _
625  byval label as FBSYMBOL ptr, _
626  byval dvreg as IRVREG ptr, _
627  byval svreg as IRVREG ptr _
628  ) as EMIT_NODE ptr
629 
630 declare function emitGE _
631  ( _
632  byval rvreg as IRVREG ptr, _
633  byval label as FBSYMBOL ptr, _
634  byval dvreg as IRVREG ptr, _
635  byval svreg as IRVREG ptr _
636  ) as EMIT_NODE ptr
637 
638 declare function emitATN2 _
639  ( _
640  byval dvreg as IRVREG ptr, _
641  byval svreg as IRVREG ptr _
642  ) as EMIT_NODE ptr
643 
644 declare function emitPOW _
645  ( _
646  byval dvreg as IRVREG ptr, _
647  byval svreg as IRVREG ptr _
648  ) as EMIT_NODE ptr
649 
650 declare function emitNEG _
651  ( _
652  byval dvreg as IRVREG ptr _
653  ) as EMIT_NODE ptr
654 
655 declare function emitNOT _
656  ( _
657  byval dvreg as IRVREG ptr _
658  ) as EMIT_NODE ptr
659 
660 declare function emitABS _
661  ( _
662  byval dvreg as IRVREG ptr _
663  ) as EMIT_NODE ptr
664 
665 declare function emitHADD _
666  ( _
667  byval dvreg as IRVREG ptr _
668  ) as EMIT_NODE ptr
669 
670 declare function emitSGN _
671  ( _
672  byval dvreg as IRVREG ptr _
673  ) as EMIT_NODE ptr
674 
675 declare function emitFIX _
676  ( _
677  byval dvreg as IRVREG ptr _
678  ) as EMIT_NODE ptr
679 
680 declare function emitFRAC _
681  ( _
682  byval dvreg as IRVREG ptr _
683  ) as EMIT_NODE ptr
684 
685 declare function emitCONVFD2FS( byval dvreg as IRVREG ptr ) as EMIT_NODE ptr
686 
687 declare function emitSWZREP _
688  ( _
689  byval dvreg as IRVREG ptr _
690  ) as EMIT_NODE ptr
691 
692 declare function emitSIN _
693  ( _
694  byval dvreg as IRVREG ptr _
695  ) as EMIT_NODE ptr
696 
697 declare function emitASIN _
698  ( _
699  byval dvreg as IRVREG ptr _
700  ) as EMIT_NODE ptr
701 
702 declare function emitCOS _
703  ( _
704  byval dvreg as IRVREG ptr _
705  ) as EMIT_NODE ptr
706 
707 declare function emitACOS _
708  ( _
709  byval dvreg as IRVREG ptr _
710  ) as EMIT_NODE ptr
711 
712 declare function emitTAN _
713  ( _
714  byval dvreg as IRVREG ptr _
715  ) as EMIT_NODE ptr
716 
717 declare function emitATAN _
718  ( _
719  byval dvreg as IRVREG ptr _
720  ) as EMIT_NODE ptr
721 
722 declare function emitSQRT _
723  ( _
724  byval dvreg as IRVREG ptr _
725  ) as EMIT_NODE ptr
726 
727 declare function emitRSQRT _
728  ( _
729  byval dvreg as IRVREG ptr _
730  ) as EMIT_NODE ptr
731 
732 declare function emitRCP _
733  ( _
734  byval dvreg as IRVREG ptr _
735  ) as EMIT_NODE ptr
736 
737 declare function emitLOG _
738  ( _
739  byval dvreg as IRVREG ptr _
740  ) as EMIT_NODE ptr
741 
742 declare function emitEXP _
743  ( _
744  byval dvreg as IRVREG ptr _
745  ) as EMIT_NODE ptr
746 
747 declare function emitFLOOR _
748  ( _
749  byval dvreg as IRVREG ptr _
750  ) as EMIT_NODE ptr
751 
752 declare function emitSTACKALIGN( byval bytes as integer ) as EMIT_NODE ptr
753 
754 declare function emitPUSH _
755  ( _
756  byval svreg as IRVREG ptr _
757  ) as EMIT_NODE ptr
758 
759 declare function emitPUSHUDT _
760  ( _
761  byval svreg as IRVREG ptr, _
762  byval sdsize as integer _
763  ) as EMIT_NODE ptr
764 
765 declare function emitPOP _
766  ( _
767  byval svreg as IRVREG ptr _
768  ) as EMIT_NODE ptr
769 
770 declare function emitMEMMOVE _
771  ( _
772  byval dvreg as IRVREG ptr, _
773  byval svreg as IRVREG ptr, _
774  byval bytes as integer _
775  ) as EMIT_NODE ptr
776 
777 declare function emitMEMSWAP _
778  ( _
779  byval dvreg as IRVREG ptr, _
780  byval svreg as IRVREG ptr, _
781  byval bytes as integer _
782  ) as EMIT_NODE ptr
783 
784 declare function emitMEMCLEAR _
785  ( _
786  byval dvreg as IRVREG ptr, _
787  byval bytes_vreg as IRVREG ptr _
788  ) as EMIT_NODE ptr
789 
790 declare function emitSTKCLEAR _
791  ( _
792  byval bytes as integer, _
793  byval baseofs as integer _
794  ) as EMIT_NODE ptr
795 
796 declare function emitDBGLineBegin _
797  ( _
798  byval proc as FBSYMBOL ptr, _
799  byval ex as integer _
800  ) as EMIT_NODE ptr
801 
802 declare function emitDBGLineEnd _
803  ( _
804  byval proc as FBSYMBOL ptr, _
805  byval ex as integer _
806  ) as EMIT_NODE ptr
807 
808 declare function emitDBGScopeBegin _
809  ( _
810  byval sym as FBSYMBOL ptr _
811  ) as EMIT_NODE ptr
812 
813 declare function emitDBGScopeEnd _
814  ( _
815  byval sym as FBSYMBOL ptr _
816  ) as EMIT_NODE ptr
817 
818 declare sub emitVARINIBEGIN( byval sym as FBSYMBOL ptr )
819 declare sub emitVARINIi( byval dtype as integer, byval value as integer )
820 declare sub emitVARINIf( byval dtype as integer, byval value as double )
821 declare sub emitVARINI64( byval dtype as integer, byval value as longint )
822 declare sub emitVARINIOFS( byval sname as zstring ptr, byval ofs as integer )
823 declare sub emitVARINISTR( byval s as const zstring ptr )
824 declare sub emitVARINIWSTR( byval s as zstring ptr )
825 declare sub emitVARINIPAD( byval bytes as integer )
826 declare sub emitFBCTINFBEGIN( )
827 declare sub emitFBCTINFSTRING( byval s as zstring ptr )
828 declare sub emitFBCTINFEND( )
829 
830 declare sub emitWriteStr _
831  ( _
832  byval s as const zstring ptr, _
833  byval addtab as integer = FALSE _
834  )
835 
836 declare sub emitWriteObjinfo _
837  ( _
838  byval liblist as TLIST ptr, _
839  byval libpathlist as TLIST ptr _
840  )
841 
842 declare sub emitReset _
843  ( _
844  _
845  )
846 
847 declare sub emitFlush _
848  ( _
849  _
850  )
851 
852 
853 #define emitGetOptionValue( opt ) emit.vtbl.getOptionValue( opt )
854 
855 #define emitIsKeyword( text ) emit.vtbl.isKeyword( text )
856 
857 #define emitOpen( ) emit.vtbl.open( )
858 
859 #define emitClose( tottime ) emit.vtbl.close( tottime )
860 
861 #define emitProcBegin( proc ) emit.vtbl.procBegin( proc )
862 
863 #define emitProcEnd( proc ) emit.vtbl.procEnd( proc )
864 
865 #define emitProcHeader( proc, initlabel ) emit.vtbl.procHeader( proc, initlabel )
866 
867 #define emitProcFooter( proc, bytestopop, initlabel, exitlabel ) emit.vtbl.procFooter( proc, bytestopop, initlabel, exitlabel )
868 
869 #define emitProcAllocArg( proc, s ) emit.vtbl.procAllocArg( proc, s )
870 
871 #define emitProcAllocLocal( proc, s ) emit.vtbl.procAllocLocal( proc, s )
872 
873 #define emitProcGetFrameRegName( ) emit.vtbl.procGetFrameRegName( )
874 
875 #define emitScopeBegin( s ) emit.vtbl.scopeBegin( s )
876 
877 #define emitScopeEnd( s ) emit.vtbl.scopeEnd( s )
878 
879 #define emitProcAllocStaticVars( head_sym ) emit.vtbl.procAllocStaticVars( head_sym )
880 
881 #define emitIsRegPreserved( dclass, reg ) emit.vtbl.isRegPreserved( dclass, reg )
882 
883 #define emitGetFreePreservedReg( dclass, dtype ) emit.vtbl.getFreePreservedReg( dclass, dtype )
884 
885 #define emitGetResultReg( dtype, dclass, reg, reg2 ) emit.vtbl.getResultReg( dtype, dclass, reg, reg2 )
886 
887 #define emitSection( sec, priority ) emit.vtbl.setSection( sec, priority )
888 
889 ''::::
890 #define EMIT_REGSETUSED(c,r) emit.regUsedTB(c) or= (1 shl r)
891 #define EMIT_REGISUSED(c,r) ((emit.regUsedTB(c) and (1 shl r)) <> 0)
892 #define EMIT_REGCLEARALL(c) emit.regUsedTB(c) = 0
893 #define EMIT_REGTRASHALL(c) emit.regUsedTB(c) = -1
894 
895 ''
896 '' inter-module globals
897 ''
898 extern emit as EMITCTX
899 
900 extern emit_opfTB(0 to EMIT_MAXOPS-1) as any ptr
901 
902 #endif '' __EMIT_BI__
903