hlcgcpu.pas 17 KB

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