hlcgcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit contains routines to create a pass-through high-level code
  5. generator. This is used by most regular code generators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit hlcgcpu;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globals,globtype,
  24. aasmdata,
  25. symtype,symdef,parabase,
  26. cgbase,cgutils,
  27. hlcgobj, hlcgx86;
  28. type
  29. { thlcgcpu }
  30. thlcgcpu = class(thlcgx86)
  31. private
  32. { checks whether the type needs special methodptr-like handling, when stored
  33. in a LOC_REGISTER location. This applies to the following types:
  34. - i8086 method pointers (incl. 6-byte mixed near + far),
  35. - 6-byte records (only in the medium and compact memory model are these
  36. loaded in a register)
  37. - nested proc ptrs
  38. When stored in a LOC_REGISTER tlocation, these types use both register
  39. and registerhi with the following sizes:
  40. register - cgsize = int_cgsize(voidcodepointertype.size)
  41. registerhi - cgsize = int_cgsize(voidpointertype.size) }
  42. function is_methodptr_like_type(d:tdef): boolean;
  43. { 4-byte records in registers need special handling as well. A record may
  44. be located in registerhi:register if it was converted from a procvar or
  45. in GetNextReg(register):register if it was converted from a longint.
  46. We can tell between the two by checking whether registerhi has been set. }
  47. function is_fourbyterecord(d:tdef): boolean;
  48. protected
  49. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint); override;
  50. public
  51. function getaddressregister(list:TAsmList;size:tdef):Tregister;override;
  52. procedure reference_reset_base(var ref: treference; regsize: tdef; reg: tregister; offset, alignment: longint); override;
  53. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; forceresdef: tdef; weak: boolean): tcgpara;override;
  54. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);override;
  55. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  56. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  57. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  58. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);override;
  59. end;
  60. procedure create_hlcodegen;
  61. implementation
  62. uses
  63. verbose,
  64. paramgr,
  65. cpubase,cpuinfo,tgobj,cgobj,cgcpu,
  66. defutil,
  67. symconst,symcpu,
  68. procinfo,fmodule,
  69. aasmcpu;
  70. { thlcgcpu }
  71. function thlcgcpu.is_methodptr_like_type(d: tdef): boolean;
  72. var
  73. is_sixbyterecord,is_methodptr,is_nestedprocptr: Boolean;
  74. begin
  75. is_sixbyterecord:=(d.typ=recorddef) and (d.size=6);
  76. is_methodptr:=(d.typ=procvardef)
  77. and (po_methodpointer in tprocvardef(d).procoptions)
  78. and not(po_addressonly in tprocvardef(d).procoptions);
  79. is_nestedprocptr:=(d.typ=procvardef)
  80. and is_nested_pd(tprocvardef(d))
  81. and not(po_addressonly in tprocvardef(d).procoptions);
  82. result:=is_sixbyterecord or is_methodptr or is_nestedprocptr;
  83. end;
  84. function thlcgcpu.is_fourbyterecord(d: tdef): boolean;
  85. begin
  86. result:=(d.typ=recorddef) and (d.size=4);
  87. end;
  88. procedure thlcgcpu.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  89. var
  90. locsize : tcgsize;
  91. tmploc : tlocation;
  92. href : treference;
  93. stacksize : longint;
  94. begin
  95. if not(l.size in [OS_32,OS_S32,OS_64,OS_S64,OS_128,OS_S128]) then
  96. locsize:=l.size
  97. else
  98. locsize:=int_float_cgsize(tcgsize2size[l.size]);
  99. case l.loc of
  100. LOC_FPUREGISTER,
  101. LOC_CFPUREGISTER:
  102. begin
  103. case cgpara.location^.loc of
  104. LOC_REFERENCE:
  105. begin
  106. stacksize:=align(locintsize,cgpara.alignment);
  107. if (not paramanager.use_fixed_stack) and
  108. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  109. begin
  110. cg.g_stackpointer_alloc(list,stacksize);
  111. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  112. end
  113. else
  114. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  115. cg.a_loadfpu_reg_ref(list,locsize,locsize,l.register,href);
  116. end;
  117. LOC_FPUREGISTER:
  118. begin
  119. cg.a_loadfpu_reg_reg(list,locsize,cgpara.location^.size,l.register,cgpara.location^.register);
  120. end;
  121. { can happen if a record with only 1 "single field" is
  122. returned in a floating point register and then is directly
  123. passed to a regcall parameter }
  124. LOC_REGISTER:
  125. begin
  126. tmploc:=l;
  127. location_force_mem(list,tmploc,size);
  128. case locsize of
  129. OS_F32:
  130. tmploc.size:=OS_32;
  131. OS_F64:
  132. tmploc.size:=OS_64;
  133. else
  134. internalerror(2010053116);
  135. end;
  136. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  137. location_freetemp(list,tmploc);
  138. end
  139. else
  140. internalerror(2010053003);
  141. end;
  142. end;
  143. LOC_MMREGISTER,
  144. LOC_CMMREGISTER:
  145. begin
  146. case cgpara.location^.loc of
  147. LOC_REFERENCE:
  148. begin
  149. { can't use TCGSize2Size[l.size], because the size of an
  150. 80 bit extended parameter can be either 10 or 12 bytes }
  151. stacksize:=align(locintsize,cgpara.alignment);
  152. if (not paramanager.use_fixed_stack) and
  153. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  154. begin
  155. cg.g_stackpointer_alloc(list,stacksize);
  156. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  157. end
  158. else
  159. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  160. cg.a_loadmm_reg_ref(list,locsize,locsize,l.register,href,mms_movescalar);
  161. end;
  162. LOC_FPUREGISTER:
  163. begin
  164. tmploc:=l;
  165. location_force_mem(list,tmploc,size);
  166. cg.a_loadfpu_ref_cgpara(list,tmploc.size,tmploc.reference,cgpara);
  167. location_freetemp(list,tmploc);
  168. end;
  169. else
  170. internalerror(2010053004);
  171. end;
  172. end;
  173. LOC_REFERENCE,
  174. LOC_CREFERENCE :
  175. begin
  176. case cgpara.location^.loc of
  177. LOC_REFERENCE:
  178. begin
  179. stacksize:=align(locintsize,cgpara.alignment);
  180. if (not paramanager.use_fixed_stack) and
  181. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  182. cg.a_load_ref_cgpara(list,locsize,l.reference,cgpara)
  183. else
  184. begin
  185. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  186. cg.g_concatcopy(list,l.reference,href,stacksize);
  187. end;
  188. end;
  189. LOC_FPUREGISTER:
  190. begin
  191. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  192. end;
  193. else
  194. internalerror(2010053005);
  195. end;
  196. end;
  197. else
  198. internalerror(2002042430);
  199. end;
  200. end;
  201. function thlcgcpu.getaddressregister(list: TAsmList; size: tdef): Tregister;
  202. begin
  203. { implicit pointer types on i8086 follow the default data pointer size for
  204. the current memory model }
  205. if is_implicit_pointer_object_type(size) or is_implicit_array_pointer(size) then
  206. size:=voidpointertype;
  207. if is_farpointer(size) or is_hugepointer(size) then
  208. Result:=cg.getintregister(list,OS_32)
  209. else
  210. Result:=cg.getintregister(list,OS_16);
  211. end;
  212. procedure thlcgcpu.reference_reset_base(var ref: treference; regsize: tdef;
  213. reg: tregister; offset, alignment: longint);
  214. begin
  215. inherited reference_reset_base(ref, regsize, reg, offset, alignment);
  216. { implicit pointer types on i8086 follow the default data pointer size for
  217. the current memory model }
  218. if is_implicit_pointer_object_type(regsize) or is_implicit_array_pointer(regsize) then
  219. regsize:=voidpointertype;
  220. if regsize.typ=pointerdef then
  221. case tcpupointerdef(regsize).x86pointertyp of
  222. x86pt_near:
  223. ;
  224. x86pt_near_cs:
  225. ref.segment:=NR_CS;
  226. x86pt_near_ds:
  227. ref.segment:=NR_DS;
  228. x86pt_near_ss:
  229. ref.segment:=NR_SS;
  230. x86pt_near_es:
  231. ref.segment:=NR_ES;
  232. x86pt_near_fs:
  233. ref.segment:=NR_FS;
  234. x86pt_near_gs:
  235. ref.segment:=NR_GS;
  236. x86pt_far,
  237. x86pt_huge:
  238. ref.segment:=GetNextReg(reg);
  239. end;
  240. end;
  241. function thlcgcpu.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; weak: boolean): tcgpara;
  242. begin
  243. if is_proc_far(pd) then
  244. begin
  245. { far calls to the same module (in $HUGECODE off mode) can be optimized
  246. to push cs + call near, because they are in the same segment }
  247. if not (cs_huge_code in current_settings.moduleswitches) and
  248. pd.owner.iscurrentunit and not (po_external in pd.procoptions) then
  249. begin
  250. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  251. tcg8086(cg).a_call_name_near(list,s,weak);
  252. end
  253. else
  254. tcg8086(cg).a_call_name_far(list,s,weak);
  255. end
  256. else
  257. tcg8086(cg).a_call_name_near(list,s,weak);
  258. result:=get_call_result_cgpara(pd,forceresdef);
  259. end;
  260. procedure thlcgcpu.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  261. var
  262. tmpref: treference;
  263. begin
  264. if is_methodptr_like_type(tosize) and (loc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  265. begin
  266. tmpref:=ref;
  267. a_load_reg_ref(list,voidcodepointertype,voidcodepointertype,loc.register,tmpref);
  268. inc(tmpref.offset,voidcodepointertype.size);
  269. a_load_reg_ref(list,voidpointertype,voidpointertype,loc.registerhi,tmpref);
  270. end
  271. else if is_fourbyterecord(tosize) and (loc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  272. begin
  273. tmpref:=ref;
  274. cg.a_load_reg_ref(list,OS_16,OS_16,loc.register,tmpref);
  275. inc(tmpref.offset,2);
  276. if loc.registerhi<>tregister(0) then
  277. cg.a_load_reg_ref(list,OS_16,OS_16,loc.registerhi,tmpref)
  278. else
  279. cg.a_load_reg_ref(list,OS_16,OS_16,GetNextReg(loc.register),tmpref);
  280. end
  281. else
  282. inherited a_load_loc_ref(list, fromsize, tosize, loc, ref);
  283. end;
  284. procedure thlcgcpu.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  285. var
  286. tmpref,segref: treference;
  287. begin
  288. { step 1: call the x86 low level code generator to handle the offset;
  289. we set the segment to NR_NO to disable the i8086 segment handling code
  290. in the low level cg (which can be removed, once all calls to
  291. a_loadaddr_ref_reg go through the high level code generator) }
  292. tmpref:=ref;
  293. tmpref.segment:=NR_NO;
  294. cg.a_loadaddr_ref_reg(list, tmpref, r);
  295. { step 2: if destination is a far pointer, we have to pass a segment as well }
  296. if is_farpointer(tosize) or is_hugepointer(tosize) then
  297. begin
  298. { if a segment register is specified in ref, we use that }
  299. if ref.segment<>NR_NO then
  300. begin
  301. if is_segment_reg(ref.segment) then
  302. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,ref.segment,GetNextReg(r)))
  303. else
  304. cg.a_load_reg_reg(list,OS_16,OS_16,ref.segment,GetNextReg(r));
  305. end
  306. { references relative to a symbol use the segment of the symbol,
  307. which can be obtained by the SEG directive }
  308. else if assigned(ref.symbol) then
  309. begin
  310. reference_reset_symbol(segref,ref.symbol,0,0);
  311. segref.refaddr:=addr_seg;
  312. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,segref,GetNextReg(r));
  313. end
  314. else if ref.base=NR_BP then
  315. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,GetNextReg(r)))
  316. else
  317. internalerror(2014032801);
  318. end;
  319. end;
  320. procedure thlcgcpu.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  321. begin
  322. if paramanager.use_fixed_stack then
  323. begin
  324. inherited;
  325. exit;
  326. end;
  327. tcg8086(cg).g_copyvaluepara_openarray(list,ref,lenloc,arrdef.elesize,destreg);
  328. end;
  329. procedure thlcgcpu.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  330. begin
  331. if paramanager.use_fixed_stack then
  332. begin
  333. inherited;
  334. exit;
  335. end;
  336. tcg8086(cg).g_releasevaluepara_openarray(list,l);
  337. end;
  338. procedure thlcgcpu.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  339. var
  340. r,tmpref: treference;
  341. begin
  342. if is_methodptr_like_type(size) and (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  343. begin
  344. tg.gethltemp(list,size,size.size,tt_normal,r);
  345. tmpref:=r;
  346. a_load_reg_ref(list,voidcodepointertype,voidcodepointertype,l.register,tmpref);
  347. inc(tmpref.offset,voidcodepointertype.size);
  348. a_load_reg_ref(list,voidpointertype,voidpointertype,l.registerhi,tmpref);
  349. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  350. l.reference:=r;
  351. end
  352. else if is_fourbyterecord(size) and (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  353. begin
  354. tg.gethltemp(list,size,size.size,tt_normal,r);
  355. tmpref:=r;
  356. cg.a_load_reg_ref(list,OS_16,OS_16,l.register,tmpref);
  357. inc(tmpref.offset,2);
  358. if l.registerhi<>tregister(0) then
  359. cg.a_load_reg_ref(list,OS_16,OS_16,l.registerhi,tmpref)
  360. else
  361. cg.a_load_reg_ref(list,OS_16,OS_16,GetNextReg(l.register),tmpref);
  362. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  363. l.reference:=r;
  364. end
  365. else
  366. inherited;
  367. end;
  368. procedure create_hlcodegen;
  369. begin
  370. hlcg:=thlcgcpu.create;
  371. create_codegen;
  372. end;
  373. end.