symcpu.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. tcpuprogramparasym = class(tprogramparasym)
  121. end;
  122. tcpuprogramparasymclass = class(tprogramparasym);
  123. tcpunamespacesym = class(tnamespacesym)
  124. end;
  125. tcpunamespacesymclass = class of tcpunamespacesym;
  126. tcpuprocsym = class(tprocsym)
  127. end;
  128. tcpuprocsymclass = class of tcpuprocsym;
  129. tcputypesym = class(ttypesym)
  130. end;
  131. tcpuypesymclass = class of tcputypesym;
  132. tcpufieldvarsym = class(tfieldvarsym)
  133. end;
  134. tcpufieldvarsymclass = class of tcpufieldvarsym;
  135. tcpulocalvarsym = class(tlocalvarsym)
  136. end;
  137. tcpulocalvarsymclass = class of tcpulocalvarsym;
  138. tcpuparavarsym = class(tparavarsym)
  139. end;
  140. tcpuparavarsymclass = class of tcpuparavarsym;
  141. tcpustaticvarsym = class(tstaticvarsym)
  142. end;
  143. tcpustaticvarsymclass = class of tcpustaticvarsym;
  144. tcpuabsolutevarsym = class(ti86absolutevarsym)
  145. protected
  146. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  147. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  148. public
  149. addrsegment : aword;
  150. end;
  151. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  152. tcpupropertysym = class(tpropertysym)
  153. end;
  154. tcpupropertysymclass = class of tcpupropertysym;
  155. tcpuconstsym = class(tconstsym)
  156. end;
  157. tcpuconstsymclass = class of tcpuconstsym;
  158. tcpuenumsym = class(tenumsym)
  159. end;
  160. tcpuenumsymclass = class of tcpuenumsym;
  161. tcpusyssym = class(tsyssym)
  162. end;
  163. tcpusyssymclass = class of tcpusyssym;
  164. const
  165. pbestrealtype : ^tdef = @s80floattype;
  166. function is_proc_far(p: tabstractprocdef): boolean;
  167. {# Returns true if p is a far pointer def }
  168. function is_farpointer(p : tdef) : boolean;
  169. {# Returns true if p is a huge pointer def }
  170. function is_hugepointer(p : tdef) : boolean;
  171. implementation
  172. uses
  173. globals, cpuinfo, verbose, fmodule;
  174. function is_proc_far(p: tabstractprocdef): boolean;
  175. begin
  176. if p is tcpuprocdef then
  177. result:=tcpuprocdef(p).is_far
  178. else if p is tcpuprocvardef then
  179. result:=tcpuprocvardef(p).is_far
  180. else
  181. internalerror(2014041301);
  182. end;
  183. { true if p is a far pointer def }
  184. function is_farpointer(p : tdef) : boolean;
  185. begin
  186. result:=(p.typ=pointerdef) and (tcpupointerdef(p).x86pointertyp=x86pt_far);
  187. end;
  188. { true if p is a huge pointer def }
  189. function is_hugepointer(p : tdef) : boolean;
  190. begin
  191. result:=(p.typ=pointerdef) and (tcpupointerdef(p).x86pointertyp=x86pt_huge);
  192. end;
  193. {****************************************************************************
  194. tcpuarraydef
  195. ****************************************************************************}
  196. constructor tcpuarraydef.create_from_pointer(def: tpointerdef);
  197. begin
  198. if tcpupointerdef(def).x86pointertyp=x86pt_huge then
  199. begin
  200. huge:=true;
  201. { use -1 so that the elecount will not overflow }
  202. self.create(0,high(asizeint)-1,s32inttype);
  203. arrayoptions:=[ado_IsConvertedPointer];
  204. setelementdef(def.pointeddef);
  205. end
  206. else
  207. begin
  208. huge:=false;
  209. inherited create_from_pointer(def);
  210. end;
  211. end;
  212. function tcpuarraydef.getcopy: tstoreddef;
  213. begin
  214. result:=inherited;
  215. tcpuarraydef(result).huge:=huge;
  216. end;
  217. function tcpuarraydef.GetTypeName: string;
  218. begin
  219. Result:=inherited;
  220. if is_huge then
  221. Result:='Huge '+Result;
  222. end;
  223. procedure tcpuarraydef.ppuload_platform(ppufile: tcompilerppufile);
  224. begin
  225. inherited;
  226. huge:=(ppufile.getbyte<>0);
  227. end;
  228. procedure tcpuarraydef.ppuwrite_platform(ppufile: tcompilerppufile);
  229. begin
  230. inherited;
  231. ppufile.putbyte(byte(huge));
  232. end;
  233. {****************************************************************************
  234. tcpuprocdef
  235. ****************************************************************************}
  236. constructor tcpuprocdef.create(level: byte);
  237. begin
  238. inherited create(level);
  239. if (current_settings.x86memorymodel in x86_far_code_models) and
  240. ((cs_huge_code in current_settings.moduleswitches) or
  241. (cs_force_far_calls in current_settings.localswitches)) then
  242. procoptions:=procoptions+[po_far];
  243. end;
  244. function tcpuprocdef.address_type: tdef;
  245. begin
  246. if is_far then
  247. result:=voidfarpointertype
  248. else
  249. result:=voidnearpointertype;
  250. end;
  251. procedure tcpuprocdef.declared_far;
  252. begin
  253. if current_settings.x86memorymodel in x86_far_code_models then
  254. include(procoptions,po_far)
  255. else
  256. inherited declared_far;
  257. end;
  258. procedure tcpuprocdef.declared_near;
  259. begin
  260. if (current_settings.x86memorymodel in x86_far_code_models) and
  261. not (cs_huge_code in current_settings.moduleswitches) then
  262. exclude(procoptions,po_far)
  263. else
  264. inherited declared_near;
  265. end;
  266. function tcpuprocdef.default_far: boolean;
  267. begin
  268. if proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize,
  269. potype_constructor,potype_destructor,
  270. potype_class_constructor,potype_class_destructor,
  271. potype_propgetter,potype_propsetter] then
  272. exit(true);
  273. if (procoptions*[po_classmethod,po_virtualmethod,po_abstractmethod,
  274. po_finalmethod,po_staticmethod,po_overridingmethod,
  275. po_external,po_public,po_interrupt])<>[] then
  276. exit(true);
  277. if is_methodpointer then
  278. exit(true);
  279. result:=not (visibility in [vis_private,vis_hidden]);
  280. end;
  281. function tcpuprocdef.is_far: boolean;
  282. begin
  283. result:=(current_settings.x86memorymodel in x86_far_code_models) and
  284. ((po_far in procoptions) or default_far);
  285. end;
  286. {****************************************************************************
  287. tcpuprocvardef
  288. ****************************************************************************}
  289. constructor tcpuprocvardef.create(level: byte);
  290. begin
  291. inherited create(level);
  292. { procvars are always far in the far code memory models }
  293. if current_settings.x86memorymodel in x86_far_code_models then
  294. procoptions:=procoptions+[po_far];
  295. end;
  296. function tcpuprocvardef.is_far: boolean;
  297. begin
  298. { procvars are always far in the far code memory models }
  299. result:=current_settings.x86memorymodel in x86_far_code_models;
  300. end;
  301. {****************************************************************************
  302. tcpupointerdef
  303. ****************************************************************************}
  304. class function tcpupointerdef.default_x86_data_pointer_type: tx86pointertyp;
  305. begin
  306. if current_settings.x86memorymodel in x86_far_data_models then
  307. result:=x86pt_far
  308. else
  309. result:=inherited;
  310. end;
  311. function tcpupointerdef.pointer_arithmetic_int_type:tdef;
  312. begin
  313. if x86pointertyp=x86pt_huge then
  314. result:=s32inttype
  315. else
  316. result:=inherited;
  317. end;
  318. function tcpupointerdef.pointer_subtraction_result_type:tdef;
  319. begin
  320. case x86pointertyp of
  321. x86pt_huge:
  322. result:=s32inttype;
  323. x86pt_far:
  324. result:=u16inttype;
  325. else
  326. result:=inherited;
  327. end;
  328. end;
  329. {****************************************************************************
  330. tcpuabsolutevarsym
  331. ****************************************************************************}
  332. procedure tcpuabsolutevarsym.ppuload_platform(ppufile: tcompilerppufile);
  333. begin
  334. inherited;
  335. if absseg then
  336. addrsegment:=ppufile.getaword;
  337. end;
  338. procedure tcpuabsolutevarsym.ppuwrite_platform(ppufile: tcompilerppufile);
  339. begin
  340. inherited;
  341. if absseg then
  342. ppufile.putaword(addrsegment);
  343. end;
  344. begin
  345. { used tdef classes }
  346. cfiledef:=tcpufiledef;
  347. cvariantdef:=tcpuvariantdef;
  348. cformaldef:=tcpuformaldef;
  349. cforwarddef:=tcpuforwarddef;
  350. cundefineddef:=tcpuundefineddef;
  351. cerrordef:=tcpuerrordef;
  352. cpointerdef:=tcpupointerdef;
  353. crecorddef:=tcpurecorddef;
  354. cimplementedinterface:=tcpuimplementedinterface;
  355. cobjectdef:=tcpuobjectdef;
  356. cclassrefdef:=tcpuclassrefdef;
  357. carraydef:=tcpuarraydef;
  358. corddef:=tcpuorddef;
  359. cfloatdef:=tcpufloatdef;
  360. cprocvardef:=tcpuprocvardef;
  361. cprocdef:=tcpuprocdef;
  362. cstringdef:=tcpustringdef;
  363. cenumdef:=tcpuenumdef;
  364. csetdef:=tcpusetdef;
  365. { used tsym classes }
  366. clabelsym:=tcpulabelsym;
  367. cunitsym:=tcpuunitsym;
  368. cprogramparasym:=tcpuprogramparasym;
  369. cnamespacesym:=tcpunamespacesym;
  370. cprocsym:=tcpuprocsym;
  371. ctypesym:=tcputypesym;
  372. cfieldvarsym:=tcpufieldvarsym;
  373. clocalvarsym:=tcpulocalvarsym;
  374. cparavarsym:=tcpuparavarsym;
  375. cstaticvarsym:=tcpustaticvarsym;
  376. cabsolutevarsym:=tcpuabsolutevarsym;
  377. cpropertysym:=tcpupropertysym;
  378. cconstsym:=tcpuconstsym;
  379. cenumsym:=tcpuenumsym;
  380. csyssym:=tcpusyssym;
  381. cPtrDefHashSet:=tx86PtrDefHashSet;
  382. end.