symx86.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; override;
  39. end;
  40. tx86pointerdefclass = class of tx86pointerdef;
  41. tx86procvardef = class(tprocvardef)
  42. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; override;
  43. end;
  44. tx86procvardefclass = class of tx86procvardef;
  45. tx86procdef = class(tprocdef)
  46. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; override;
  47. end;
  48. tx86procdefclass = class of tx86procdef;
  49. implementation
  50. uses
  51. globals, verbose,
  52. symbase, fmodule;
  53. {****************************************************************************
  54. tx86pointerdef
  55. ****************************************************************************}
  56. procedure tx86pointerdef.ppuload_platform(ppufile: tcompilerppufile);
  57. begin
  58. inherited;
  59. x86pointertyp:=tx86pointertyp(ppufile.getbyte);
  60. end;
  61. procedure tx86pointerdef.ppuwrite_platform(ppufile: tcompilerppufile);
  62. begin
  63. inherited;
  64. ppufile.putbyte(byte(x86pointertyp));
  65. end;
  66. constructor tx86pointerdef.create(def: tdef);
  67. begin
  68. inherited create(def);
  69. x86pointertyp := default_x86_data_pointer_type;
  70. end;
  71. class function tx86pointerdef.getreusable(def: tdef): tpointerdef;
  72. begin
  73. result:=getreusablex86(def,default_x86_data_pointer_type);
  74. end;
  75. class function tx86pointerdef.getreusablex86(def: tdef; x86typ: tx86pointertyp): tpointerdef;
  76. type
  77. tx86PtrDefKey = packed record
  78. def: tdef;
  79. x86typ:tx86pointertyp;
  80. end;
  81. var
  82. res: PHashSetItem;
  83. oldsymtablestack: tsymtablestack;
  84. key: tx86PtrDefKey;
  85. begin
  86. if not assigned(current_module) then
  87. internalerror(2011071101);
  88. key.def:=def;
  89. key.x86typ:=x86typ;
  90. res:=current_module.ptrdefs.FindOrAdd(@key,sizeof(key));
  91. if not assigned(res^.Data) then
  92. begin
  93. { since these pointerdefs can be reused anywhere in the current
  94. unit, add them to the global/staticsymtable (or local symtable
  95. if they're a local def, because otherwise they'll be saved
  96. to the ppu referencing a local symtable entry that doesn't
  97. exist in the ppu) }
  98. oldsymtablestack:=symtablestack;
  99. { do not simply push/pop current_module.localsymtable, because
  100. that can have side-effects (e.g., it removes helpers) }
  101. symtablestack:=nil;
  102. res^.Data:=tx86pointerdefclass(cpointerdef).createx86(def,x86typ);
  103. def.getreusablesymtab.insertdef(tdef(res^.Data));
  104. symtablestack:=oldsymtablestack;
  105. end;
  106. result:=tpointerdef(res^.Data);
  107. end;
  108. constructor tx86pointerdef.createx86(def: tdef; x86typ: tx86pointertyp);
  109. begin
  110. inherited create(def);
  111. x86pointertyp:=x86typ;
  112. end;
  113. function tx86pointerdef.size: asizeint;
  114. begin
  115. if x86pointertyp in [x86pt_far,x86pt_huge] then
  116. result:=sizeof(pint)+2
  117. else
  118. result:=inherited;
  119. end;
  120. function tx86pointerdef.getcopy: tstoreddef;
  121. begin
  122. result:=inherited;
  123. tx86pointerdef(result).x86pointertyp:=x86pointertyp;
  124. end;
  125. function tx86pointerdef.GetTypeName: string;
  126. begin
  127. result:=inherited;
  128. if x86pointertyp<>default_x86_data_pointer_type then
  129. begin
  130. case x86pointertyp of
  131. x86pt_near:
  132. result:=result+';near';
  133. x86pt_near_cs:
  134. result:=result+';near ''CS''';
  135. x86pt_near_ds:
  136. result:=result+';near ''DS''';
  137. x86pt_near_ss:
  138. result:=result+';near ''SS''';
  139. x86pt_near_es:
  140. result:=result+';near ''ES''';
  141. x86pt_near_fs:
  142. result:=result+';near ''FS''';
  143. x86pt_near_gs:
  144. result:=result+';near ''GS''';
  145. x86pt_far:
  146. result:=result+';far';
  147. x86pt_huge:
  148. result:=result+';huge';
  149. else
  150. internalerror(2013050301);
  151. end;
  152. end;
  153. end;
  154. class function tx86pointerdef.default_x86_data_pointer_type: tx86pointertyp;
  155. begin
  156. result:=x86pt_near;
  157. end;
  158. function tx86pointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  159. begin
  160. result:=
  161. inherited and
  162. (x86pointertyp=tx86pointerdef(ptr).x86pointertyp);
  163. end;
  164. {****************************************************************************
  165. tx86procvardef
  166. ****************************************************************************}
  167. function tx86procvardef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  168. begin
  169. result:=
  170. inherited and
  171. (tx86pointerdef(voidcodepointertype).x86pointertyp=tx86pointerdef(ptr).x86pointertyp);
  172. end;
  173. {****************************************************************************
  174. tx86procdef
  175. ****************************************************************************}
  176. function tx86procdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  177. begin
  178. result:=
  179. inherited and
  180. (tx86pointerdef(voidcodepointertype).x86pointertyp=tx86pointerdef(ptr).x86pointertyp);
  181. end;
  182. end.