symx86.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. {
  2. Copyright (c) 2014 by Florian Klaempfl
  3. Symbol table overrides for x86
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symx86;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype, cclasses,
  22. symconst, symtype,symdef,symsym;
  23. type
  24. tx86pointerdef = class(tpointerdef)
  25. protected
  26. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  27. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  28. public
  29. x86pointertyp : tx86pointertyp;
  30. constructor create(def: tdef); override;
  31. class function getreusable(def: tdef): tpointerdef; override;
  32. class function getreusablex86(def: tdef; x86typ: tx86pointertyp): tpointerdef;
  33. constructor createx86(def:tdef;x86typ:tx86pointertyp);virtual;
  34. function size: asizeint; override;
  35. function getcopy: tstoreddef; override;
  36. function GetTypeName: string; override;
  37. class function default_x86_data_pointer_type: tx86pointertyp; virtual;
  38. end;
  39. tx86pointerdefclass = class of tx86pointerdef;
  40. implementation
  41. uses
  42. globals, verbose,
  43. symbase, fmodule;
  44. {****************************************************************************
  45. tx86pointerdef
  46. ****************************************************************************}
  47. procedure tx86pointerdef.ppuload_platform(ppufile: tcompilerppufile);
  48. begin
  49. inherited;
  50. x86pointertyp:=tx86pointertyp(ppufile.getbyte);
  51. end;
  52. procedure tx86pointerdef.ppuwrite_platform(ppufile: tcompilerppufile);
  53. begin
  54. inherited;
  55. ppufile.putbyte(byte(x86pointertyp));
  56. end;
  57. constructor tx86pointerdef.create(def: tdef);
  58. begin
  59. inherited create(def);
  60. x86pointertyp := default_x86_data_pointer_type;
  61. end;
  62. class function tx86pointerdef.getreusable(def: tdef): tpointerdef;
  63. begin
  64. result:=getreusablex86(def,default_x86_data_pointer_type);
  65. end;
  66. class function tx86pointerdef.getreusablex86(def: tdef; x86typ: tx86pointertyp): tpointerdef;
  67. type
  68. tx86PtrDefKey = packed record
  69. def: tdef;
  70. x86typ:tx86pointertyp;
  71. end;
  72. var
  73. res: PHashSetItem;
  74. oldsymtablestack: tsymtablestack;
  75. key: tx86PtrDefKey;
  76. begin
  77. if not assigned(current_module) then
  78. internalerror(2011071101);
  79. key.def:=def;
  80. key.x86typ:=x86typ;
  81. res:=current_module.ptrdefs.FindOrAdd(@key,sizeof(key));
  82. if not assigned(res^.Data) then
  83. begin
  84. { since these pointerdefs can be reused anywhere in the current
  85. unit, add them to the global/staticsymtable (or local symtable
  86. if they're a local def, because otherwise they'll be saved
  87. to the ppu referencing a local symtable entry that doesn't
  88. exist in the ppu) }
  89. oldsymtablestack:=symtablestack;
  90. { do not simply push/pop current_module.localsymtable, because
  91. that can have side-effects (e.g., it removes helpers) }
  92. symtablestack:=nil;
  93. res^.Data:=tx86pointerdefclass(cpointerdef).createx86(def,x86typ);
  94. def.getreusablesymtab.insertdef(tdef(res^.Data));
  95. symtablestack:=oldsymtablestack;
  96. end;
  97. result:=tpointerdef(res^.Data);
  98. end;
  99. constructor tx86pointerdef.createx86(def: tdef; x86typ: tx86pointertyp);
  100. begin
  101. tabstractpointerdef(self).create(pointerdef,def);
  102. x86pointertyp := x86typ;
  103. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  104. end;
  105. function tx86pointerdef.size: asizeint;
  106. begin
  107. if x86pointertyp in [x86pt_far,x86pt_huge] then
  108. result:=sizeof(pint)+2
  109. else
  110. result:=inherited;
  111. end;
  112. function tx86pointerdef.getcopy: tstoreddef;
  113. begin
  114. result:=inherited;
  115. tx86pointerdef(result).x86pointertyp:=x86pointertyp;
  116. end;
  117. function tx86pointerdef.GetTypeName: string;
  118. begin
  119. result:=inherited;
  120. if x86pointertyp<>default_x86_data_pointer_type then
  121. begin
  122. case x86pointertyp of
  123. x86pt_near:
  124. result:=result+';near';
  125. x86pt_near_cs:
  126. result:=result+';near ''CS''';
  127. x86pt_near_ds:
  128. result:=result+';near ''DS''';
  129. x86pt_near_ss:
  130. result:=result+';near ''SS''';
  131. x86pt_near_es:
  132. result:=result+';near ''ES''';
  133. x86pt_near_fs:
  134. result:=result+';near ''FS''';
  135. x86pt_near_gs:
  136. result:=result+';near ''GS''';
  137. x86pt_far:
  138. result:=result+';far';
  139. x86pt_huge:
  140. result:=result+';huge';
  141. else
  142. internalerror(2013050301);
  143. end;
  144. end;
  145. end;
  146. class function tx86pointerdef.default_x86_data_pointer_type: tx86pointertyp;
  147. begin
  148. result:=x86pt_near;
  149. end;
  150. end.