symx86.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. result:=tx86pointerdefclass(cpointerdef).createx86(def,x86typ);
  103. setup_reusable_def(def,result,res,oldsymtablestack);
  104. { res^.Data may still be nil -> don't overwrite result }
  105. exit;
  106. end;
  107. result:=tpointerdef(res^.Data);
  108. end;
  109. constructor tx86pointerdef.createx86(def: tdef; x86typ: tx86pointertyp);
  110. begin
  111. inherited create(def);
  112. x86pointertyp:=x86typ;
  113. end;
  114. function tx86pointerdef.size: asizeint;
  115. begin
  116. if x86pointertyp in [x86pt_far,x86pt_huge] then
  117. result:=sizeof(pint)+2
  118. else
  119. result:=inherited;
  120. end;
  121. function tx86pointerdef.getcopy: tstoreddef;
  122. begin
  123. result:=inherited;
  124. tx86pointerdef(result).x86pointertyp:=x86pointertyp;
  125. end;
  126. function tx86pointerdef.GetTypeName: string;
  127. begin
  128. result:=inherited;
  129. if x86pointertyp<>default_x86_data_pointer_type then
  130. begin
  131. case x86pointertyp of
  132. x86pt_near:
  133. result:=result+';near';
  134. x86pt_near_cs:
  135. result:=result+';near ''CS''';
  136. x86pt_near_ds:
  137. result:=result+';near ''DS''';
  138. x86pt_near_ss:
  139. result:=result+';near ''SS''';
  140. x86pt_near_es:
  141. result:=result+';near ''ES''';
  142. x86pt_near_fs:
  143. result:=result+';near ''FS''';
  144. x86pt_near_gs:
  145. result:=result+';near ''GS''';
  146. x86pt_far:
  147. result:=result+';far';
  148. x86pt_huge:
  149. result:=result+';huge';
  150. else
  151. internalerror(2013050301);
  152. end;
  153. end;
  154. end;
  155. class function tx86pointerdef.default_x86_data_pointer_type: tx86pointertyp;
  156. begin
  157. result:=x86pt_near;
  158. end;
  159. function tx86pointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  160. begin
  161. result:=
  162. inherited and
  163. (x86pointertyp=tx86pointerdef(ptr).x86pointertyp);
  164. end;
  165. {****************************************************************************
  166. tx86procvardef
  167. ****************************************************************************}
  168. function tx86procvardef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  169. begin
  170. result:=
  171. inherited and
  172. (tx86pointerdef(address_type).x86pointertyp=tx86pointerdef(ptr).x86pointertyp);
  173. end;
  174. {****************************************************************************
  175. tx86procdef
  176. ****************************************************************************}
  177. function tx86procdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  178. begin
  179. result:=
  180. inherited and
  181. (tx86pointerdef(address_type).x86pointertyp=tx86pointerdef(ptr).x86pointertyp);
  182. end;
  183. end.