hlcgcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. end;
  183. Result:=inherited a_call_name(list, pd, s, paras, forceresdef, weak);
  184. { Free EBX }
  185. if need_got_load then
  186. ungetcpuregister(list, NR_PIC_OFFSET_REG);
  187. end;
  188. procedure thlcgcpu.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  189. begin
  190. if paramanager.use_fixed_stack then
  191. begin
  192. inherited;
  193. exit;
  194. end;
  195. tcg386(cg).g_copyvaluepara_openarray(list,ref,lenloc,arrdef.elesize,destreg);
  196. end;
  197. procedure thlcgcpu.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  198. begin
  199. if paramanager.use_fixed_stack then
  200. begin
  201. inherited;
  202. exit;
  203. end;
  204. tcg386(cg).g_releasevaluepara_openarray(list,l);
  205. end;
  206. procedure thlcgcpu.g_exception_reason_save(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const href: treference);
  207. begin
  208. if not paramanager.use_fixed_stack then
  209. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[def_cgsize(tosize)],reg))
  210. else
  211. inherited
  212. end;
  213. procedure thlcgcpu.g_exception_reason_save_const(list: TAsmList; size: tdef; a: tcgint; const href: treference);
  214. begin
  215. if not paramanager.use_fixed_stack then
  216. list.concat(Taicpu.op_const(A_PUSH,tcgsize2opsize[def_cgsize(size)],a))
  217. else
  218. inherited;
  219. end;
  220. procedure thlcgcpu.g_exception_reason_load(list: TAsmList; fromsize, tosize: tdef; const href: treference; reg: tregister);
  221. begin
  222. if not paramanager.use_fixed_stack then
  223. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[def_cgsize(tosize)],reg))
  224. else
  225. inherited;
  226. end;
  227. procedure thlcgcpu.g_exception_reason_discard(list: TAsmList; size: tdef; href: treference);
  228. begin
  229. if not paramanager.use_fixed_stack then
  230. begin
  231. getcpuregister(list,NR_FUNCTION_RESULT_REG);
  232. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[def_cgsize(size)],NR_FUNCTION_RESULT_REG));
  233. ungetcpuregister(list,NR_FUNCTION_RESULT_REG);
  234. end;
  235. end;
  236. procedure thlcgcpu.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  237. {
  238. possible calling conventions:
  239. default stdcall cdecl pascal register
  240. default(0): OK OK OK OK OK
  241. virtual(1): OK OK OK OK OK(2 or 1)
  242. (0):
  243. set self parameter to correct value
  244. jmp mangledname
  245. (1): The wrapper code use %ecx to reach the virtual method address
  246. set self to correct value
  247. move self,%eax
  248. mov 0(%eax),%ecx ; load vmt
  249. jmp vmtoffs(%ecx) ; method offs
  250. (2): Virtual use values pushed on stack to reach the method address
  251. so the following code be generated:
  252. set self to correct value
  253. push %ebx ; allocate space for function address
  254. push %eax
  255. mov self,%eax
  256. mov 0(%eax),%eax ; load vmt
  257. mov vmtoffs(%eax),eax ; method offs
  258. mov %eax,4(%esp)
  259. pop %eax
  260. ret 0; jmp the address
  261. }
  262. { returns whether ECX is used (either as a parameter or is nonvolatile and shouldn't be changed) }
  263. function is_ecx_used: boolean;
  264. var
  265. i: Integer;
  266. hp: tparavarsym;
  267. paraloc: PCGParaLocation;
  268. begin
  269. if not (RS_ECX in paramanager.get_volatile_registers_int(procdef.proccalloption)) then
  270. exit(true);
  271. for i:=0 to procdef.paras.count-1 do
  272. begin
  273. hp:=tparavarsym(procdef.paras[i]);
  274. procdef.init_paraloc_info(calleeside);
  275. paraloc:=hp.paraloc[calleeside].Location;
  276. while paraloc<>nil do
  277. begin
  278. if (paraloc^.Loc=LOC_REGISTER) and (getsupreg(paraloc^.register)=RS_ECX) then
  279. exit(true);
  280. paraloc:=paraloc^.Next;
  281. end;
  282. end;
  283. Result:=false;
  284. end;
  285. procedure getselftoeax(offs: longint);
  286. var
  287. href : treference;
  288. selfoffsetfromsp : longint;
  289. begin
  290. { mov offset(%esp),%eax }
  291. if (procdef.proccalloption<>pocall_register) then
  292. begin
  293. { framepointer is pushed for nested procs }
  294. if procdef.parast.symtablelevel>normal_function_level then
  295. selfoffsetfromsp:=2*sizeof(aint)
  296. else
  297. selfoffsetfromsp:=sizeof(aint);
  298. reference_reset_base(href,voidstackpointertype,NR_ESP,selfoffsetfromsp+offs,ctempposinvalid,4,[]);
  299. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  300. end;
  301. end;
  302. procedure loadvmtto(reg: tregister);
  303. var
  304. href : treference;
  305. begin
  306. { mov 0(%eax),%reg ; load vmt}
  307. reference_reset_base(href,voidpointertype,NR_EAX,0,ctempposinvalid,4,[]);
  308. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,reg);
  309. end;
  310. procedure op_onregmethodaddr(op: TAsmOp; reg: tregister);
  311. var
  312. href : treference;
  313. begin
  314. if (procdef.extnumber=$ffff) then
  315. Internalerror(200006139);
  316. { call/jmp vmtoffs(%reg) ; method offs }
  317. reference_reset_base(href,voidpointertype,reg,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),ctempposinvalid,4,[]);
  318. list.concat(taicpu.op_ref(op,S_L,href));
  319. end;
  320. procedure loadmethodoffstoeax;
  321. var
  322. href : treference;
  323. begin
  324. if (procdef.extnumber=$ffff) then
  325. Internalerror(200006139);
  326. { mov vmtoffs(%eax),%eax ; method offs }
  327. reference_reset_base(href,voidpointertype,NR_EAX,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),ctempposinvalid,4,[]);
  328. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  329. end;
  330. var
  331. lab : tasmsymbol;
  332. make_global : boolean;
  333. href : treference;
  334. begin
  335. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  336. Internalerror(200006137);
  337. if not assigned(procdef.struct) or
  338. (procdef.procoptions*[po_classmethod, po_staticmethod,
  339. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  340. Internalerror(200006138);
  341. if procdef.owner.symtabletype<>ObjectSymtable then
  342. Internalerror(200109191);
  343. make_global:=false;
  344. if (not current_module.is_unit) or
  345. create_smartlink or
  346. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  347. make_global:=true;
  348. if make_global then
  349. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0,procdef))
  350. else
  351. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0,procdef));
  352. { set param1 interface to self }
  353. g_adjust_self_value(list,procdef,ioffset);
  354. if (po_virtualmethod in procdef.procoptions) and
  355. not is_objectpascal_helper(procdef.struct) then
  356. begin
  357. if (procdef.proccalloption=pocall_register) and is_ecx_used then
  358. begin
  359. { case 2 }
  360. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EBX)); { allocate space for address}
  361. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  362. getselftoeax(8);
  363. loadvmtto(NR_EAX);
  364. loadmethodoffstoeax;
  365. { mov %eax,4(%esp) }
  366. reference_reset_base(href,voidstackpointertype,NR_ESP,4,ctempposinvalid,4,[]);
  367. list.concat(taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  368. { pop %eax }
  369. list.concat(taicpu.op_reg(A_POP,S_L,NR_EAX));
  370. { ret ; jump to the address }
  371. list.concat(taicpu.op_none(A_RET,S_L));
  372. end
  373. else
  374. begin
  375. { case 1 }
  376. getselftoeax(0);
  377. loadvmtto(NR_ECX);
  378. op_onregmethodaddr(A_JMP,NR_ECX);
  379. end;
  380. end
  381. { case 0 }
  382. else
  383. begin
  384. if (target_info.system <> system_i386_darwin) then
  385. begin
  386. lab:=current_asmdata.RefAsmSymbol(procdef.mangledname,AT_FUNCTION);
  387. list.concat(taicpu.op_sym(A_JMP,S_NO,lab))
  388. end
  389. else
  390. list.concat(taicpu.op_sym(A_JMP,S_NO,tcgx86(cg).get_darwin_call_stub(procdef.mangledname,false)))
  391. end;
  392. List.concat(Tai_symbol_end.Createname(labelname));
  393. end;
  394. procedure create_hlcodegen;
  395. begin
  396. hlcg:=thlcgcpu.create;
  397. create_codegen;
  398. end;
  399. begin
  400. chlcgobj:=thlcgcpu;
  401. end.