hlcgcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. globtype,
  24. aasmdata,
  25. symtype,symdef,parabase,
  26. cgbase,cgutils,
  27. hlcgobj, hlcgx86;
  28. type
  29. thlcgcpu = class(thlcgx86)
  30. protected
  31. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint); override;
  32. public
  33. function a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara; override;
  34. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  35. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  36. procedure g_exception_reason_save(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const href: treference); override;
  37. procedure g_exception_reason_save_const(list: TAsmList; size: tdef; a: tcgint; const href: treference); override;
  38. procedure g_exception_reason_load(list: TAsmList; fromsize, tosize: tdef; const href: treference; reg: tregister); override;
  39. procedure g_exception_reason_discard(list: TAsmList; size: tdef; href: treference); override;
  40. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  41. end;
  42. procedure create_hlcodegen;
  43. implementation
  44. uses
  45. globals, procinfo,
  46. verbose,
  47. fmodule,systems,
  48. aasmbase,aasmtai,
  49. paramgr,
  50. symconst,symsym,defutil,
  51. cpubase,aasmcpu,tgobj,cgobj,cgx86,cgcpu;
  52. { thlcgcpu }
  53. procedure thlcgcpu.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  54. var
  55. locsize : tcgsize;
  56. tmploc : tlocation;
  57. href : treference;
  58. stacksize : longint;
  59. begin
  60. if not(l.size in [OS_32,OS_S32,OS_64,OS_S64,OS_128,OS_S128]) then
  61. locsize:=l.size
  62. else
  63. locsize:=int_float_cgsize(tcgsize2size[l.size]);
  64. case l.loc of
  65. LOC_FPUREGISTER,
  66. LOC_CFPUREGISTER:
  67. begin
  68. case cgpara.location^.loc of
  69. LOC_REFERENCE:
  70. begin
  71. stacksize:=align(locintsize,cgpara.alignment);
  72. if (not paramanager.use_fixed_stack) and
  73. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  74. begin
  75. cg.g_stackpointer_alloc(list,stacksize);
  76. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,ctempposinvalid,voidstackpointertype.size,[]);
  77. end
  78. else
  79. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  80. cg.a_loadfpu_reg_ref(list,locsize,locsize,l.register,href);
  81. end;
  82. LOC_FPUREGISTER:
  83. begin
  84. cg.a_loadfpu_reg_reg(list,locsize,cgpara.location^.size,l.register,cgpara.location^.register);
  85. end;
  86. { can happen if a record with only 1 "single field" is
  87. returned in a floating point register and then is directly
  88. passed to a regcall parameter }
  89. LOC_REGISTER:
  90. begin
  91. tmploc:=l;
  92. location_force_mem(list,tmploc,size);
  93. case locsize of
  94. OS_F32:
  95. tmploc.size:=OS_32;
  96. OS_F64:
  97. tmploc.size:=OS_64;
  98. else
  99. internalerror(2010053116);
  100. end;
  101. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  102. location_freetemp(list,tmploc);
  103. end
  104. else
  105. internalerror(2010053003);
  106. end;
  107. end;
  108. LOC_MMREGISTER,
  109. LOC_CMMREGISTER:
  110. begin
  111. case cgpara.location^.loc of
  112. LOC_REFERENCE:
  113. begin
  114. { can't use TCGSize2Size[l.size], because the size of an
  115. 80 bit extended parameter can be either 10 or 12 bytes }
  116. stacksize:=align(locintsize,cgpara.alignment);
  117. if (not paramanager.use_fixed_stack) and
  118. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  119. begin
  120. cg.g_stackpointer_alloc(list,stacksize);
  121. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,ctempposinvalid,voidstackpointertype.size,[]);
  122. end
  123. else
  124. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  125. cg.a_loadmm_reg_ref(list,locsize,locsize,l.register,href,mms_movescalar);
  126. end;
  127. LOC_FPUREGISTER:
  128. begin
  129. tmploc:=l;
  130. location_force_mem(list,tmploc,size);
  131. cg.a_loadfpu_ref_cgpara(list,tmploc.size,tmploc.reference,cgpara);
  132. location_freetemp(list,tmploc);
  133. end;
  134. else
  135. internalerror(2010053004);
  136. end;
  137. end;
  138. LOC_REFERENCE,
  139. LOC_CREFERENCE :
  140. begin
  141. case cgpara.location^.loc of
  142. LOC_REFERENCE:
  143. begin
  144. stacksize:=align(locintsize,cgpara.alignment);
  145. if (not paramanager.use_fixed_stack) and
  146. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  147. cg.a_load_ref_cgpara(list,locsize,l.reference,cgpara)
  148. else
  149. begin
  150. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  151. cg.g_concatcopy(list,l.reference,href,stacksize);
  152. end;
  153. end;
  154. LOC_FPUREGISTER:
  155. begin
  156. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  157. end;
  158. else
  159. internalerror(2010053005);
  160. end;
  161. end;
  162. else
  163. internalerror(2002042430);
  164. end;
  165. end;
  166. function thlcgcpu.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  167. var
  168. need_got_load: boolean;
  169. begin
  170. { Load GOT address to EBX before calling an external function.
  171. It is needed because GOT stubs for external function calls
  172. generated by a linker expect EBX as a GOT register. }
  173. need_got_load:=not (target_info.system in systems_darwin) and
  174. (cs_create_pic in current_settings.moduleswitches) and
  175. (tf_pic_uses_got in target_info.flags) and
  176. (po_external in pd.procoptions);
  177. if need_got_load then
  178. begin
  179. { Alloc EBX }
  180. getcpuregister(list, NR_PIC_OFFSET_REG);
  181. list.concat(taicpu.op_reg_reg(A_MOV,S_L,current_procinfo.got,NR_PIC_OFFSET_REG));
  182. include(current_procinfo.flags,pi_needs_got);
  183. end;
  184. Result:=inherited a_call_name(list, pd, s, paras, forceresdef, weak);
  185. { Free EBX }
  186. if need_got_load then
  187. ungetcpuregister(list, NR_PIC_OFFSET_REG);
  188. end;
  189. procedure thlcgcpu.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  190. begin
  191. if paramanager.use_fixed_stack then
  192. begin
  193. inherited;
  194. exit;
  195. end;
  196. tcg386(cg).g_copyvaluepara_openarray(list,ref,lenloc,arrdef.elesize,destreg);
  197. end;
  198. procedure thlcgcpu.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  199. begin
  200. if paramanager.use_fixed_stack then
  201. begin
  202. inherited;
  203. exit;
  204. end;
  205. tcg386(cg).g_releasevaluepara_openarray(list,l);
  206. end;
  207. procedure thlcgcpu.g_exception_reason_save(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const href: treference);
  208. begin
  209. if not paramanager.use_fixed_stack then
  210. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[def_cgsize(tosize)],reg))
  211. else
  212. inherited
  213. end;
  214. procedure thlcgcpu.g_exception_reason_save_const(list: TAsmList; size: tdef; a: tcgint; const href: treference);
  215. begin
  216. if not paramanager.use_fixed_stack then
  217. list.concat(Taicpu.op_const(A_PUSH,tcgsize2opsize[def_cgsize(size)],a))
  218. else
  219. inherited;
  220. end;
  221. procedure thlcgcpu.g_exception_reason_load(list: TAsmList; fromsize, tosize: tdef; const href: treference; reg: tregister);
  222. begin
  223. if not paramanager.use_fixed_stack then
  224. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[def_cgsize(tosize)],reg))
  225. else
  226. inherited;
  227. end;
  228. procedure thlcgcpu.g_exception_reason_discard(list: TAsmList; size: tdef; href: treference);
  229. begin
  230. if not paramanager.use_fixed_stack then
  231. begin
  232. getcpuregister(list,NR_FUNCTION_RESULT_REG);
  233. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[def_cgsize(size)],NR_FUNCTION_RESULT_REG));
  234. ungetcpuregister(list,NR_FUNCTION_RESULT_REG);
  235. end;
  236. end;
  237. procedure thlcgcpu.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  238. {
  239. possible calling conventions:
  240. default stdcall cdecl pascal register
  241. default(0): OK OK OK OK OK
  242. virtual(1): OK OK OK OK OK(2 or 1)
  243. (0):
  244. set self parameter to correct value
  245. jmp mangledname
  246. (1): The wrapper code use %ecx to reach the virtual method address
  247. set self to correct value
  248. move self,%eax
  249. mov 0(%eax),%ecx ; load vmt
  250. jmp vmtoffs(%ecx) ; method offs
  251. (2): Virtual use values pushed on stack to reach the method address
  252. so the following code be generated:
  253. set self to correct value
  254. push %ebx ; allocate space for function address
  255. push %eax
  256. mov self,%eax
  257. mov 0(%eax),%eax ; load vmt
  258. mov vmtoffs(%eax),eax ; method offs
  259. mov %eax,4(%esp)
  260. pop %eax
  261. ret 0; jmp the address
  262. }
  263. { returns whether ECX is used (either as a parameter or is nonvolatile and shouldn't be changed) }
  264. function is_ecx_used: boolean;
  265. var
  266. i: Integer;
  267. hp: tparavarsym;
  268. paraloc: PCGParaLocation;
  269. begin
  270. if not (RS_ECX in paramanager.get_volatile_registers_int(procdef.proccalloption)) then
  271. exit(true);
  272. for i:=0 to procdef.paras.count-1 do
  273. begin
  274. hp:=tparavarsym(procdef.paras[i]);
  275. procdef.init_paraloc_info(calleeside);
  276. paraloc:=hp.paraloc[calleeside].Location;
  277. while paraloc<>nil do
  278. begin
  279. if (paraloc^.Loc=LOC_REGISTER) and (getsupreg(paraloc^.register)=RS_ECX) then
  280. exit(true);
  281. paraloc:=paraloc^.Next;
  282. end;
  283. end;
  284. Result:=false;
  285. end;
  286. procedure getselftoeax(offs: longint);
  287. var
  288. href : treference;
  289. selfoffsetfromsp : longint;
  290. begin
  291. { mov offset(%esp),%eax }
  292. if (procdef.proccalloption<>pocall_register) then
  293. begin
  294. { framepointer is pushed for nested procs }
  295. if procdef.parast.symtablelevel>normal_function_level then
  296. selfoffsetfromsp:=2*sizeof(aint)
  297. else
  298. selfoffsetfromsp:=sizeof(aint);
  299. reference_reset_base(href,voidstackpointertype,NR_ESP,selfoffsetfromsp+offs,ctempposinvalid,4,[]);
  300. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  301. end;
  302. end;
  303. procedure loadvmtto(reg: tregister);
  304. var
  305. href : treference;
  306. begin
  307. { mov 0(%eax),%reg ; load vmt}
  308. reference_reset_base(href,voidpointertype,NR_EAX,0,ctempposinvalid,4,[]);
  309. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,reg);
  310. end;
  311. procedure op_onregmethodaddr(op: TAsmOp; reg: tregister);
  312. var
  313. href : treference;
  314. begin
  315. if (procdef.extnumber=$ffff) then
  316. Internalerror(200006139);
  317. { call/jmp vmtoffs(%reg) ; method offs }
  318. reference_reset_base(href,voidpointertype,reg,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),ctempposinvalid,4,[]);
  319. list.concat(taicpu.op_ref(op,S_L,href));
  320. end;
  321. procedure loadmethodoffstoeax;
  322. var
  323. href : treference;
  324. begin
  325. if (procdef.extnumber=$ffff) then
  326. Internalerror(200006139);
  327. { mov vmtoffs(%eax),%eax ; method offs }
  328. reference_reset_base(href,voidpointertype,NR_EAX,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),ctempposinvalid,4,[]);
  329. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  330. end;
  331. var
  332. lab : tasmsymbol;
  333. make_global : boolean;
  334. href : treference;
  335. begin
  336. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  337. Internalerror(200006137);
  338. if not assigned(procdef.struct) or
  339. (procdef.procoptions*[po_classmethod, po_staticmethod,
  340. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  341. Internalerror(200006138);
  342. if procdef.owner.symtabletype<>ObjectSymtable then
  343. Internalerror(200109191);
  344. make_global:=false;
  345. if (not current_module.is_unit) or
  346. create_smartlink or
  347. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  348. make_global:=true;
  349. if make_global then
  350. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0,procdef))
  351. else
  352. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0,procdef));
  353. { set param1 interface to self }
  354. g_adjust_self_value(list,procdef,ioffset);
  355. if (po_virtualmethod in procdef.procoptions) and
  356. not is_objectpascal_helper(procdef.struct) then
  357. begin
  358. if (procdef.proccalloption=pocall_register) and is_ecx_used then
  359. begin
  360. { case 2 }
  361. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EBX)); { allocate space for address}
  362. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  363. getselftoeax(8);
  364. loadvmtto(NR_EAX);
  365. loadmethodoffstoeax;
  366. { mov %eax,4(%esp) }
  367. reference_reset_base(href,voidstackpointertype,NR_ESP,4,ctempposinvalid,4,[]);
  368. list.concat(taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  369. { pop %eax }
  370. list.concat(taicpu.op_reg(A_POP,S_L,NR_EAX));
  371. { ret ; jump to the address }
  372. list.concat(taicpu.op_none(A_RET,S_L));
  373. end
  374. else
  375. begin
  376. { case 1 }
  377. getselftoeax(0);
  378. loadvmtto(NR_ECX);
  379. op_onregmethodaddr(A_JMP,NR_ECX);
  380. end;
  381. end
  382. { case 0 }
  383. else
  384. begin
  385. if (target_info.system <> system_i386_darwin) then
  386. begin
  387. lab:=current_asmdata.RefAsmSymbol(procdef.mangledname,AT_FUNCTION);
  388. list.concat(taicpu.op_sym(A_JMP,S_NO,lab))
  389. end
  390. else
  391. list.concat(taicpu.op_sym(A_JMP,S_NO,tcgx86(cg).get_darwin_call_stub(procdef.mangledname,false)))
  392. end;
  393. List.concat(Tai_symbol_end.Createname(labelname));
  394. end;
  395. procedure create_hlcodegen;
  396. begin
  397. hlcg:=thlcgcpu.create;
  398. create_codegen;
  399. end;
  400. begin
  401. chlcgobj:=thlcgcpu;
  402. end.