symcpu.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. {
  2. Copyright (c) 2014 by Florian Klaempfl
  3. Symbol table overrides for i8086
  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 symcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symconst,symtype,symdef,symsym,symx86,symi86;
  23. type
  24. { defs }
  25. tcpufiledef = class(tfiledef)
  26. end;
  27. tcpufiledefclass = class of tcpufiledef;
  28. tcpuvariantdef = class(tvariantdef)
  29. end;
  30. tcpuvariantdefclass = class of tcpuvariantdef;
  31. tcpuformaldef = class(tformaldef)
  32. end;
  33. tcpuformaldefclass = class of tcpuformaldef;
  34. tcpuforwarddef = class(tforwarddef)
  35. end;
  36. tcpuforwarddefclass = class of tcpuforwarddef;
  37. tcpuundefineddef = class(tundefineddef)
  38. end;
  39. tcpuundefineddefclass = class of tcpuundefineddef;
  40. tcpuerrordef = class(terrordef)
  41. end;
  42. tcpuerrordefclass = class of tcpuerrordef;
  43. tcpupointerdef = class(tx86pointerdef)
  44. class function default_x86_data_pointer_type: tx86pointertyp; override;
  45. function pointer_arithmetic_int_type:tdef; override;
  46. function pointer_subtraction_result_type:tdef; override;
  47. end;
  48. tcpupointerdefclass = class of tcpupointerdef;
  49. tcpurecorddef = class(trecorddef)
  50. end;
  51. tcpurecorddefclass = class of tcpurecorddef;
  52. tcpuimplementedinterface = class(timplementedinterface)
  53. end;
  54. tcpuimplementedinterfaceclass = class of tcpuimplementedinterface;
  55. tcpuobjectdef = class(tobjectdef)
  56. end;
  57. tcpuobjectdefclass = class of tcpuobjectdef;
  58. tcpuclassrefdef = class(tclassrefdef)
  59. end;
  60. tcpuclassrefdefclass = class of tcpuclassrefdef;
  61. { tcpuarraydef }
  62. tcpuarraydef = class(tarraydef)
  63. private
  64. huge: Boolean;
  65. protected
  66. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  67. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  68. public
  69. constructor create_from_pointer(def:tpointerdef);override;
  70. function getcopy: tstoreddef; override;
  71. function GetTypeName:string;override;
  72. property is_huge: Boolean read huge write huge;
  73. end;
  74. tcpuarraydefclass = class of tcpuarraydef;
  75. tcpuorddef = class(torddef)
  76. end;
  77. tcpuorddefclass = class of tcpuorddef;
  78. tcpufloatdef = class(tfloatdef)
  79. end;
  80. tcpufloatdefclass = class of tcpufloatdef;
  81. { tcpuprocvardef }
  82. tcpuprocvardef = class(ti86procvardef)
  83. constructor create(level:byte);override;
  84. function is_far:boolean;
  85. end;
  86. tcpuprocvardefclass = class of tcpuprocvardef;
  87. { tcpuprocdef }
  88. tcpuprocdef = class(ti86procdef)
  89. private
  90. { returns whether the function is far by default, i.e. whether it would be
  91. far if _all_ of the following conditions are true:
  92. - we're in a far code memory model
  93. - it has no 'near' or 'far' specifiers
  94. - it is compiled in a $F- state }
  95. function default_far:boolean;
  96. public
  97. constructor create(level:byte);override;
  98. function address_type:tdef;override;
  99. procedure declared_far;override;
  100. procedure declared_near;override;
  101. function is_far:boolean;
  102. end;
  103. tcpuprocdefclass = class of tcpuprocdef;
  104. tcpustringdef = class(tstringdef)
  105. end;
  106. tcpustringdefclass = class of tcpustringdef;
  107. tcpuenumdef = class(tenumdef)
  108. end;
  109. tcpuenumdefclass = class of tcpuenumdef;
  110. tcpusetdef = class(tsetdef)
  111. end;
  112. tcpusetdefclass = class of tcpusetdef;
  113. { syms }
  114. tcpulabelsym = class(tlabelsym)
  115. end;
  116. tcpulabelsymclass = class of tcpulabelsym;
  117. tcpuunitsym = class(tunitsym)
  118. end;
  119. tcpuunitsymclass = class of tcpuunitsym;
  120. tcpunamespacesym = class(tnamespacesym)
  121. end;
  122. tcpunamespacesymclass = class of tcpunamespacesym;
  123. tcpuprocsym = class(tprocsym)
  124. end;
  125. tcpuprocsymclass = class of tcpuprocsym;
  126. tcputypesym = class(ttypesym)
  127. end;
  128. tcpuypesymclass = class of tcputypesym;
  129. tcpufieldvarsym = class(tfieldvarsym)
  130. end;
  131. tcpufieldvarsymclass = class of tcpufieldvarsym;
  132. tcpulocalvarsym = class(tlocalvarsym)
  133. end;
  134. tcpulocalvarsymclass = class of tcpulocalvarsym;
  135. tcpuparavarsym = class(tparavarsym)
  136. end;
  137. tcpuparavarsymclass = class of tcpuparavarsym;
  138. tcpustaticvarsym = class(tstaticvarsym)
  139. end;
  140. tcpustaticvarsymclass = class of tcpustaticvarsym;
  141. tcpuabsolutevarsym = class(ti86absolutevarsym)
  142. protected
  143. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  144. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  145. public
  146. addrsegment : aword;
  147. end;
  148. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  149. tcpupropertysym = class(tpropertysym)
  150. end;
  151. tcpupropertysymclass = class of tcpupropertysym;
  152. tcpuconstsym = class(tconstsym)
  153. end;
  154. tcpuconstsymclass = class of tcpuconstsym;
  155. tcpuenumsym = class(tenumsym)
  156. end;
  157. tcpuenumsymclass = class of tcpuenumsym;
  158. tcpusyssym = class(tsyssym)
  159. end;
  160. tcpusyssymclass = class of tcpusyssym;
  161. const
  162. pbestrealtype : ^tdef = @s80floattype;
  163. function is_proc_far(p: tabstractprocdef): boolean;
  164. {# Returns true if p is a far pointer def }
  165. function is_farpointer(p : tdef) : boolean;
  166. {# Returns true if p is a huge pointer def }
  167. function is_hugepointer(p : tdef) : boolean;
  168. implementation
  169. uses
  170. globals, cpuinfo, verbose, fmodule;
  171. function is_proc_far(p: tabstractprocdef): boolean;
  172. begin
  173. if p is tcpuprocdef then
  174. result:=tcpuprocdef(p).is_far
  175. else if p is tcpuprocvardef then
  176. result:=tcpuprocvardef(p).is_far
  177. else
  178. internalerror(2014041301);
  179. end;
  180. { true if p is a far pointer def }
  181. function is_farpointer(p : tdef) : boolean;
  182. begin
  183. result:=(p.typ=pointerdef) and (tcpupointerdef(p).x86pointertyp=x86pt_far);
  184. end;
  185. { true if p is a huge pointer def }
  186. function is_hugepointer(p : tdef) : boolean;
  187. begin
  188. result:=(p.typ=pointerdef) and (tcpupointerdef(p).x86pointertyp=x86pt_huge);
  189. end;
  190. {****************************************************************************
  191. tcpuarraydef
  192. ****************************************************************************}
  193. constructor tcpuarraydef.create_from_pointer(def: tpointerdef);
  194. begin
  195. if tcpupointerdef(def).x86pointertyp=x86pt_huge then
  196. begin
  197. huge:=true;
  198. { use -1 so that the elecount will not overflow }
  199. self.create(0,high(asizeint)-1,s32inttype);
  200. arrayoptions:=[ado_IsConvertedPointer];
  201. setelementdef(def.pointeddef);
  202. end
  203. else
  204. begin
  205. huge:=false;
  206. inherited create_from_pointer(def);
  207. end;
  208. end;
  209. function tcpuarraydef.getcopy: tstoreddef;
  210. begin
  211. result:=inherited;
  212. tcpuarraydef(result).huge:=huge;
  213. end;
  214. function tcpuarraydef.GetTypeName: string;
  215. begin
  216. Result:=inherited;
  217. if is_huge then
  218. Result:='Huge '+Result;
  219. end;
  220. procedure tcpuarraydef.ppuload_platform(ppufile: tcompilerppufile);
  221. begin
  222. inherited;
  223. huge:=(ppufile.getbyte<>0);
  224. end;
  225. procedure tcpuarraydef.ppuwrite_platform(ppufile: tcompilerppufile);
  226. begin
  227. inherited;
  228. ppufile.putbyte(byte(huge));
  229. end;
  230. {****************************************************************************
  231. tcpuprocdef
  232. ****************************************************************************}
  233. constructor tcpuprocdef.create(level: byte);
  234. begin
  235. inherited create(level);
  236. if (current_settings.x86memorymodel in x86_far_code_models) and
  237. ((cs_huge_code in current_settings.moduleswitches) or
  238. (cs_force_far_calls in current_settings.localswitches)) then
  239. procoptions:=procoptions+[po_far];
  240. end;
  241. function tcpuprocdef.address_type: tdef;
  242. begin
  243. if is_far then
  244. result:=voidfarpointertype
  245. else
  246. result:=voidnearpointertype;
  247. end;
  248. procedure tcpuprocdef.declared_far;
  249. begin
  250. if current_settings.x86memorymodel in x86_far_code_models then
  251. include(procoptions,po_far)
  252. else
  253. inherited declared_far;
  254. end;
  255. procedure tcpuprocdef.declared_near;
  256. begin
  257. if (current_settings.x86memorymodel in x86_far_code_models) and
  258. not (cs_huge_code in current_settings.moduleswitches) then
  259. exclude(procoptions,po_far)
  260. else
  261. inherited declared_near;
  262. end;
  263. function tcpuprocdef.default_far: boolean;
  264. begin
  265. if proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize,
  266. potype_constructor,potype_destructor,
  267. potype_class_constructor,potype_class_destructor,
  268. potype_propgetter,potype_propsetter] then
  269. exit(true);
  270. if (procoptions*[po_classmethod,po_virtualmethod,po_abstractmethod,
  271. po_finalmethod,po_staticmethod,po_overridingmethod,
  272. po_external,po_public,po_interrupt])<>[] then
  273. exit(true);
  274. if is_methodpointer then
  275. exit(true);
  276. result:=not (visibility in [vis_private,vis_hidden]);
  277. end;
  278. function tcpuprocdef.is_far: boolean;
  279. begin
  280. result:=(current_settings.x86memorymodel in x86_far_code_models) and
  281. ((po_far in procoptions) or default_far);
  282. end;
  283. {****************************************************************************
  284. tcpuprocvardef
  285. ****************************************************************************}
  286. constructor tcpuprocvardef.create(level: byte);
  287. begin
  288. inherited create(level);
  289. { procvars are always far in the far code memory models }
  290. if current_settings.x86memorymodel in x86_far_code_models then
  291. procoptions:=procoptions+[po_far];
  292. end;
  293. function tcpuprocvardef.is_far: boolean;
  294. begin
  295. { procvars are always far in the far code memory models }
  296. result:=current_settings.x86memorymodel in x86_far_code_models;
  297. end;
  298. {****************************************************************************
  299. tcpupointerdef
  300. ****************************************************************************}
  301. class function tcpupointerdef.default_x86_data_pointer_type: tx86pointertyp;
  302. begin
  303. if current_settings.x86memorymodel in x86_far_data_models then
  304. result:=x86pt_far
  305. else
  306. result:=inherited;
  307. end;
  308. function tcpupointerdef.pointer_arithmetic_int_type:tdef;
  309. begin
  310. if x86pointertyp=x86pt_huge then
  311. result:=s32inttype
  312. else
  313. result:=inherited;
  314. end;
  315. function tcpupointerdef.pointer_subtraction_result_type:tdef;
  316. begin
  317. case x86pointertyp of
  318. x86pt_huge:
  319. result:=s32inttype;
  320. x86pt_far:
  321. result:=u16inttype;
  322. else
  323. result:=inherited;
  324. end;
  325. end;
  326. {****************************************************************************
  327. tcpuabsolutevarsym
  328. ****************************************************************************}
  329. procedure tcpuabsolutevarsym.ppuload_platform(ppufile: tcompilerppufile);
  330. begin
  331. inherited;
  332. if absseg then
  333. addrsegment:=ppufile.getaword;
  334. end;
  335. procedure tcpuabsolutevarsym.ppuwrite_platform(ppufile: tcompilerppufile);
  336. begin
  337. inherited;
  338. if absseg then
  339. ppufile.putaword(addrsegment);
  340. end;
  341. begin
  342. { used tdef classes }
  343. cfiledef:=tcpufiledef;
  344. cvariantdef:=tcpuvariantdef;
  345. cformaldef:=tcpuformaldef;
  346. cforwarddef:=tcpuforwarddef;
  347. cundefineddef:=tcpuundefineddef;
  348. cerrordef:=tcpuerrordef;
  349. cpointerdef:=tcpupointerdef;
  350. crecorddef:=tcpurecorddef;
  351. cimplementedinterface:=tcpuimplementedinterface;
  352. cobjectdef:=tcpuobjectdef;
  353. cclassrefdef:=tcpuclassrefdef;
  354. carraydef:=tcpuarraydef;
  355. corddef:=tcpuorddef;
  356. cfloatdef:=tcpufloatdef;
  357. cprocvardef:=tcpuprocvardef;
  358. cprocdef:=tcpuprocdef;
  359. cstringdef:=tcpustringdef;
  360. cenumdef:=tcpuenumdef;
  361. csetdef:=tcpusetdef;
  362. { used tsym classes }
  363. clabelsym:=tcpulabelsym;
  364. cunitsym:=tcpuunitsym;
  365. cnamespacesym:=tcpunamespacesym;
  366. cprocsym:=tcpuprocsym;
  367. ctypesym:=tcputypesym;
  368. cfieldvarsym:=tcpufieldvarsym;
  369. clocalvarsym:=tcpulocalvarsym;
  370. cparavarsym:=tcpuparavarsym;
  371. cstaticvarsym:=tcpustaticvarsym;
  372. cabsolutevarsym:=tcpuabsolutevarsym;
  373. cpropertysym:=tcpupropertysym;
  374. cconstsym:=tcpuconstsym;
  375. cenumsym:=tcpuenumsym;
  376. csyssym:=tcpusyssym;
  377. cPtrDefHashSet:=tx86PtrDefHashSet;
  378. end.