hlcgcpu.pas 15 KB

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