cgcpu.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the i386
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cgbase,cgobj,cg64f32,cgx86,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. cpubase,parabase,cgutils,
  25. symconst,symdef,symsym
  26. ;
  27. type
  28. tcg386 = class(tcgx86)
  29. procedure init_register_allocators;override;
  30. procedure do_register_allocation(list:TAsmList;headertai:tai);override;
  31. { passing parameter using push instead of mov }
  32. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  33. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  34. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  35. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  36. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  37. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  38. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  39. procedure g_exception_reason_save(list : TAsmList; const href : treference);override;
  40. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);override;
  41. procedure g_exception_reason_load(list : TAsmList; const href : treference);override;
  42. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  43. procedure g_maybe_got_init(list: TAsmList); override;
  44. end;
  45. tcg64f386 = class(tcg64f32)
  46. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  47. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  48. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  49. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);override;
  50. private
  51. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  52. end;
  53. procedure create_codegen;
  54. implementation
  55. uses
  56. globals,verbose,systems,cutils,
  57. paramgr,procinfo,fmodule,
  58. rgcpu,rgx86,cpuinfo;
  59. function use_push(const cgpara:tcgpara):boolean;
  60. begin
  61. result:=(not paramanager.use_fixed_stack) and
  62. assigned(cgpara.location) and
  63. (cgpara.location^.loc=LOC_REFERENCE) and
  64. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  65. end;
  66. procedure tcg386.init_register_allocators;
  67. begin
  68. inherited init_register_allocators;
  69. if not(target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  70. (cs_create_pic in current_settings.moduleswitches) then
  71. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP])
  72. else
  73. if (cs_useebp in current_settings.optimizerswitches) and assigned(current_procinfo) and (current_procinfo.framepointer<>NR_EBP) then
  74. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_EBX,RS_ESI,RS_EDI,RS_EBP],first_int_imreg,[])
  75. else
  76. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_EBX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP]);
  77. rg[R_INTREGISTER].define_class(R_SUBD,[RS_EAX,RS_EDX,RS_ECX,RS_EBX,RS_ESI,RS_EDI,RS_EBP]);
  78. rg[R_INTREGISTER].define_class(R_SUBW,[RS_AX,RS_DX,RS_CX,RS_BX,RS_DI,RS_SI,RS_BP]);
  79. rg[R_INTREGISTER].define_class(R_SUBL,[RS_AL,RS_DL,RS_CL,RS_BL]);
  80. rg[R_INTREGISTER].define_class(R_SUBH,[RS_AH,RS_DH,RS_CH,RS_BH]);
  81. rg[R_INTREGISTER].define_alias(RS_EAX,[RS_AX,RS_AL,RS_AH]);
  82. rg[R_INTREGISTER].define_alias(RS_EDX,[RS_DX,RS_DL,RS_DH]);
  83. rg[R_INTREGISTER].define_alias(RS_ECX,[RS_CX,RS_CL,RS_CH]);
  84. rg[R_INTREGISTER].define_alias(RS_EBX,[RS_BX,RS_BL,RS_BH]);
  85. rg[R_INTREGISTER].define_alias(RS_AX,[RS_EAX,RS_AL,RS_AH]);
  86. rg[R_INTREGISTER].define_alias(RS_DX,[RS_EDX,RS_DL,RS_DH]);
  87. rg[R_INTREGISTER].define_alias(RS_CX,[RS_ECX,RS_CL,RS_CH]);
  88. rg[R_INTREGISTER].define_alias(RS_BX,[RS_EBX,RS_BL,RS_BH]);
  89. rg[R_INTREGISTER].define_alias(RS_AL,[RS_EAX,RS_AX,RS_AH]);
  90. rg[R_INTREGISTER].define_alias(RS_DL,[RS_EDX,RS_DX,RS_DH]);
  91. rg[R_INTREGISTER].define_alias(RS_CL,[RS_ECX,RS_CX,RS_CH]);
  92. rg[R_INTREGISTER].define_alias(RS_BL,[RS_EBX,RS_BX,RS_BH]);
  93. rg[R_INTREGISTER].define_alias(RS_AH,[RS_EAX,RS_AL,RS_AX]);
  94. rg[R_INTREGISTER].define_alias(RS_DH,[RS_EDX,RS_DL,RS_DX]);
  95. rg[R_INTREGISTER].define_alias(RS_CH,[RS_ECX,RS_CL,RS_CX]);
  96. rg[R_INTREGISTER].define_alias(RS_BH,[RS_EBX,RS_BL,RS_BX]);
  97. rg[R_INTREGISTER].define_alias(RS_EDI,[RS_DI]);
  98. rg[R_INTREGISTER].define_alias(RS_DI,[RS_EDI]);
  99. rg[R_INTREGISTER].define_alias(RS_ESI,[RS_SI]);
  100. rg[R_INTREGISTER].define_alias(RS_SI,[RS_ESI]);
  101. rg[R_INTREGISTER].define_alias(RS_ESP,[RS_SP]);
  102. rg[R_INTREGISTER].define_alias(RS_SP,[RS_ESP]);
  103. rg[R_INTREGISTER].define_alias(RS_EBP,[RS_BP]);
  104. rg[R_INTREGISTER].define_alias(RS_BP,[RS_EBP]);
  105. rg[R_MMXREGISTER]:=trgcpu.create(R_MMXREGISTER,R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_mm_imreg,[]);
  106. rg[R_MMXREGISTER].define_class(R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7]);
  107. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBWHOLE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_mm_imreg,[]);
  108. rg[R_MMREGISTER].define_class(R_SUBWHOLE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7]);
  109. rgfpu:=Trgx86fpu.create;
  110. end;
  111. procedure tcg386.do_register_allocation(list:TAsmList;headertai:tai);
  112. begin
  113. if (pi_needs_got in current_procinfo.flags) then
  114. begin
  115. if getsupreg(current_procinfo.got) < first_int_imreg then
  116. include(rg[R_INTREGISTER].used_in_proc,getsupreg(current_procinfo.got));
  117. end;
  118. inherited do_register_allocation(list,headertai);
  119. end;
  120. procedure tcg386.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  121. var
  122. pushsize : tcgsize;
  123. begin
  124. check_register_size(size,r);
  125. if use_push(cgpara) then
  126. begin
  127. cgpara.check_simple_location;
  128. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  129. pushsize:=cgpara.location^.size
  130. else
  131. pushsize:=int_cgsize(cgpara.alignment);
  132. list.concat(taicpu.op_reg(A_PUSH,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize)));
  133. end
  134. else
  135. inherited a_load_reg_cgpara(list,size,r,cgpara);
  136. end;
  137. procedure tcg386.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  138. var
  139. pushsize : tcgsize;
  140. begin
  141. if use_push(cgpara) then
  142. begin
  143. cgpara.check_simple_location;
  144. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  145. pushsize:=cgpara.location^.size
  146. else
  147. pushsize:=int_cgsize(cgpara.alignment);
  148. list.concat(taicpu.op_const(A_PUSH,tcgsize2opsize[pushsize],a));
  149. end
  150. else
  151. inherited a_load_const_cgpara(list,size,a,cgpara);
  152. end;
  153. procedure tcg386.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  154. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  155. var
  156. pushsize : tcgsize;
  157. opsize : topsize;
  158. tmpreg : tregister;
  159. href : treference;
  160. begin
  161. if not assigned(paraloc) then
  162. exit;
  163. if (paraloc^.loc<>LOC_REFERENCE) or
  164. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  165. (tcgsize2size[paraloc^.size]>sizeof(aint)) then
  166. internalerror(200501162);
  167. { Pushes are needed in reverse order, add the size of the
  168. current location to the offset where to load from. This
  169. prevents wrong calculations for the last location when
  170. the size is not a power of 2 }
  171. if assigned(paraloc^.next) then
  172. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  173. { Push the data starting at ofs }
  174. href:=r;
  175. inc(href.offset,ofs);
  176. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  177. pushsize:=paraloc^.size
  178. else
  179. pushsize:=int_cgsize(cgpara.alignment);
  180. opsize:=TCgsize2opsize[pushsize];
  181. { for go32v2 we obtain OS_F32,
  182. but pushs is not valid, we need pushl }
  183. if opsize=S_FS then
  184. opsize:=S_L;
  185. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  186. begin
  187. tmpreg:=getintregister(list,pushsize);
  188. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  189. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  190. end
  191. else
  192. begin
  193. make_simple_ref(list,href);
  194. list.concat(taicpu.op_ref(A_PUSH,opsize,href));
  195. end;
  196. end;
  197. var
  198. len : tcgint;
  199. href : treference;
  200. begin
  201. { cgpara.size=OS_NO requires a copy on the stack }
  202. if use_push(cgpara) then
  203. begin
  204. { Record copy? }
  205. if (cgpara.size=OS_NO) or (size=OS_NO) then
  206. begin
  207. cgpara.check_simple_location;
  208. len:=align(cgpara.intsize,cgpara.alignment);
  209. g_stackpointer_alloc(list,len);
  210. reference_reset_base(href,NR_STACK_POINTER_REG,0,4);
  211. g_concatcopy(list,r,href,len);
  212. end
  213. else
  214. begin
  215. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  216. internalerror(200501161);
  217. if (cgpara.size=OS_F64) then
  218. begin
  219. href:=r;
  220. make_simple_ref(list,href);
  221. inc(href.offset,4);
  222. list.concat(taicpu.op_ref(A_PUSH,S_L,href));
  223. dec(href.offset,4);
  224. list.concat(taicpu.op_ref(A_PUSH,S_L,href));
  225. end
  226. else
  227. { We need to push the data in reverse order,
  228. therefor we use a recursive algorithm }
  229. pushdata(cgpara.location,0);
  230. end
  231. end
  232. else
  233. inherited a_load_ref_cgpara(list,size,r,cgpara);
  234. end;
  235. procedure tcg386.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  236. var
  237. tmpreg : tregister;
  238. opsize : topsize;
  239. tmpref : treference;
  240. begin
  241. with r do
  242. begin
  243. if use_push(cgpara) then
  244. begin
  245. cgpara.check_simple_location;
  246. opsize:=tcgsize2opsize[OS_ADDR];
  247. if (segment=NR_NO) and (base=NR_NO) and (index=NR_NO) then
  248. begin
  249. if assigned(symbol) then
  250. begin
  251. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  252. ((r.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  253. (cs_create_pic in current_settings.moduleswitches)) then
  254. begin
  255. tmpreg:=getaddressregister(list);
  256. a_loadaddr_ref_reg(list,r,tmpreg);
  257. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  258. end
  259. else if cs_create_pic in current_settings.moduleswitches then
  260. begin
  261. if offset<>0 then
  262. begin
  263. tmpreg:=getaddressregister(list);
  264. a_loadaddr_ref_reg(list,r,tmpreg);
  265. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  266. end
  267. else
  268. begin
  269. reference_reset_symbol(tmpref,r.symbol,0,r.alignment);
  270. tmpref.refaddr:=addr_pic;
  271. tmpref.base:=current_procinfo.got;
  272. {$ifdef EXTDEBUG}
  273. if not (pi_needs_got in current_procinfo.flags) then
  274. Comment(V_warning,'pi_needs_got not included');
  275. {$endif EXTDEBUG}
  276. include(current_procinfo.flags,pi_needs_got);
  277. list.concat(taicpu.op_ref(A_PUSH,S_L,tmpref));
  278. end
  279. end
  280. else
  281. list.concat(Taicpu.Op_sym_ofs(A_PUSH,opsize,symbol,offset));
  282. end
  283. else
  284. list.concat(Taicpu.Op_const(A_PUSH,opsize,offset));
  285. end
  286. else if (segment=NR_NO) and (base=NR_NO) and (index<>NR_NO) and
  287. (offset=0) and (scalefactor=0) and (symbol=nil) then
  288. list.concat(Taicpu.Op_reg(A_PUSH,opsize,index))
  289. else if (segment=NR_NO) and (base<>NR_NO) and (index=NR_NO) and
  290. (offset=0) and (symbol=nil) then
  291. list.concat(Taicpu.Op_reg(A_PUSH,opsize,base))
  292. else
  293. begin
  294. tmpreg:=getaddressregister(list);
  295. a_loadaddr_ref_reg(list,r,tmpreg);
  296. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  297. end;
  298. end
  299. else
  300. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  301. end;
  302. end;
  303. procedure tcg386.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  304. procedure increase_sp(a : tcgint);
  305. var
  306. href : treference;
  307. begin
  308. reference_reset_base(href,NR_STACK_POINTER_REG,a,0);
  309. { normally, lea is a better choice than an add }
  310. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  311. end;
  312. begin
  313. { Release PIC register }
  314. if (cs_create_pic in current_settings.moduleswitches) and
  315. (tf_pic_uses_got in target_info.flags) and
  316. (pi_needs_got in current_procinfo.flags) and
  317. not(target_info.system in systems_darwin) then
  318. list.concat(tai_regalloc.dealloc(NR_PIC_OFFSET_REG,nil));
  319. { MMX needs to call EMMS }
  320. if assigned(rg[R_MMXREGISTER]) and
  321. (rg[R_MMXREGISTER].uses_registers) then
  322. list.concat(Taicpu.op_none(A_EMMS,S_NO));
  323. { remove stackframe }
  324. if not nostackframe then
  325. begin
  326. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  327. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  328. begin
  329. if current_procinfo.final_localsize<>0 then
  330. increase_sp(current_procinfo.final_localsize);
  331. if (not paramanager.use_fixed_stack) then
  332. internal_restore_regs(list,true);
  333. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  334. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  335. end
  336. else
  337. begin
  338. if (not paramanager.use_fixed_stack) then
  339. internal_restore_regs(list,not (pi_has_stack_allocs in current_procinfo.flags));
  340. list.concat(Taicpu.op_none(A_LEAVE,S_NO));
  341. end;
  342. list.concat(tai_regalloc.dealloc(current_procinfo.framepointer,nil));
  343. end;
  344. { return from proc }
  345. if (po_interrupt in current_procinfo.procdef.procoptions) and
  346. { this messes up stack alignment }
  347. (target_info.stackalign=4) then
  348. begin
  349. if assigned(current_procinfo.procdef.funcretloc[calleeside].location) and
  350. (current_procinfo.procdef.funcretloc[calleeside].location^.loc=LOC_REGISTER) then
  351. begin
  352. if (getsupreg(current_procinfo.procdef.funcretloc[calleeside].location^.register)=RS_EAX) then
  353. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  354. else
  355. internalerror(2010053001);
  356. end
  357. else
  358. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EAX));
  359. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EBX));
  360. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ECX));
  361. if (current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64]) and
  362. assigned(current_procinfo.procdef.funcretloc[calleeside].location) and
  363. assigned(current_procinfo.procdef.funcretloc[calleeside].location^.next) and
  364. (current_procinfo.procdef.funcretloc[calleeside].location^.next^.loc=LOC_REGISTER) then
  365. begin
  366. if (getsupreg(current_procinfo.procdef.funcretloc[calleeside].location^.next^.register)=RS_EDX) then
  367. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  368. else
  369. internalerror(2010053002);
  370. end
  371. else
  372. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  373. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ESI));
  374. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDI));
  375. { .... also the segment registers }
  376. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  377. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_ES));
  378. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_FS));
  379. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_GS));
  380. { this restores the flags }
  381. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  382. end
  383. { Routines with the poclearstack flag set use only a ret }
  384. else if (current_procinfo.procdef.proccalloption in clearstack_pocalls) and
  385. (not paramanager.use_fixed_stack) then
  386. begin
  387. { complex return values are removed from stack in C code PM }
  388. { but not on win32 }
  389. { and not for safecall with hidden exceptions, because the result }
  390. { wich contains the exception is passed in EAX }
  391. if ((target_info.system <> system_i386_win32) or
  392. (target_info.abi=abi_old_win32_gnu)) and
  393. not ((current_procinfo.procdef.proccalloption = pocall_safecall) and
  394. (tf_safecall_exceptions in target_info.flags)) and
  395. paramanager.ret_in_param(current_procinfo.procdef.returndef,
  396. current_procinfo.procdef) then
  397. list.concat(Taicpu.Op_const(A_RET,S_W,sizeof(aint)))
  398. else
  399. list.concat(Taicpu.Op_none(A_RET,S_NO));
  400. end
  401. { ... also routines with parasize=0 }
  402. else if (parasize=0) then
  403. list.concat(Taicpu.Op_none(A_RET,S_NO))
  404. else
  405. begin
  406. { parameters are limited to 65535 bytes because ret allows only imm16 }
  407. if (parasize>65535) then
  408. CGMessage(cg_e_parasize_too_big);
  409. list.concat(Taicpu.Op_const(A_RET,S_W,parasize));
  410. end;
  411. end;
  412. procedure tcg386.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  413. var
  414. power : longint;
  415. opsize : topsize;
  416. {$ifndef __NOWINPECOFF__}
  417. again,ok : tasmlabel;
  418. {$endif}
  419. begin
  420. { get stack space }
  421. getcpuregister(list,NR_EDI);
  422. a_load_loc_reg(list,OS_INT,lenloc,NR_EDI);
  423. list.concat(Taicpu.op_reg(A_INC,S_L,NR_EDI));
  424. { Now EDI contains (high+1). }
  425. { special case handling for elesize=8, 4 and 2:
  426. set ECX = (high+1) instead of ECX = (high+1)*elesize.
  427. In the case of elesize=4 and 2, this allows us to avoid the SHR later.
  428. In the case of elesize=8, we can later use a SHL ECX, 1 instead of
  429. SHR ECX, 2 which is one byte shorter. }
  430. if (elesize=8) or (elesize=4) or (elesize=2) then
  431. begin
  432. { Now EDI contains (high+1). Copy it to ECX for later use. }
  433. getcpuregister(list,NR_ECX);
  434. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,NR_EDI,NR_ECX));
  435. end;
  436. { EDI := EDI * elesize }
  437. if (elesize<>1) then
  438. begin
  439. if ispowerof2(elesize, power) then
  440. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,NR_EDI))
  441. else
  442. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,NR_EDI));
  443. end;
  444. if (elesize<>8) and (elesize<>4) and (elesize<>2) then
  445. begin
  446. { Now EDI contains (high+1)*elesize. Copy it to ECX for later use. }
  447. getcpuregister(list,NR_ECX);
  448. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,NR_EDI,NR_ECX));
  449. end;
  450. {$ifndef __NOWINPECOFF__}
  451. { windows guards only a few pages for stack growing, }
  452. { so we have to access every page first }
  453. if target_info.system=system_i386_win32 then
  454. begin
  455. current_asmdata.getjumplabel(again);
  456. current_asmdata.getjumplabel(ok);
  457. a_label(list,again);
  458. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,NR_EDI));
  459. a_jmp_cond(list,OC_B,ok);
  460. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  461. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  462. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,NR_EDI));
  463. a_jmp_always(list,again);
  464. a_label(list,ok);
  465. end;
  466. {$endif __NOWINPECOFF__}
  467. { If we were probing pages, EDI=(size mod pagesize) and ESP is decremented
  468. by (size div pagesize)*pagesize, otherwise EDI=size.
  469. Either way, subtracting EDI from ESP will set ESP to desired final value. }
  470. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,NR_EDI,NR_ESP));
  471. { align stack on 4 bytes }
  472. list.concat(Taicpu.op_const_reg(A_AND,S_L,aint($fffffff4),NR_ESP));
  473. { load destination, don't use a_load_reg_reg, that will add a move instruction
  474. that can confuse the reg allocator }
  475. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,NR_ESP,NR_EDI));
  476. { Allocate ESI and load it with source }
  477. getcpuregister(list,NR_ESI);
  478. a_loadaddr_ref_reg(list,ref,NR_ESI);
  479. { calculate size }
  480. opsize:=S_B;
  481. if elesize=8 then
  482. begin
  483. opsize:=S_L;
  484. { ECX is number of qwords, convert to dwords }
  485. list.concat(Taicpu.op_const_reg(A_SHL,S_L,1,NR_ECX))
  486. end
  487. else if elesize=4 then
  488. begin
  489. opsize:=S_L;
  490. { ECX is already number of dwords, so no need to SHL/SHR }
  491. end
  492. else if elesize=2 then
  493. begin
  494. opsize:=S_W;
  495. { ECX is already number of words, so no need to SHL/SHR }
  496. end
  497. else
  498. if (elesize and 3)=0 then
  499. begin
  500. opsize:=S_L;
  501. { ECX is number of bytes, convert to dwords }
  502. list.concat(Taicpu.op_const_reg(A_SHR,S_L,2,NR_ECX))
  503. end
  504. else
  505. if (elesize and 1)=0 then
  506. begin
  507. opsize:=S_W;
  508. { ECX is number of bytes, convert to words }
  509. list.concat(Taicpu.op_const_reg(A_SHR,S_L,1,NR_ECX))
  510. end;
  511. if ts_cld in current_settings.targetswitches then
  512. list.concat(Taicpu.op_none(A_CLD,S_NO));
  513. list.concat(Taicpu.op_none(A_REP,S_NO));
  514. case opsize of
  515. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  516. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  517. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  518. end;
  519. ungetcpuregister(list,NR_EDI);
  520. ungetcpuregister(list,NR_ECX);
  521. ungetcpuregister(list,NR_ESI);
  522. { patch the new address, but don't use a_load_reg_reg, that will add a move instruction
  523. that can confuse the reg allocator }
  524. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,NR_ESP,destreg));
  525. include(current_procinfo.flags,pi_has_stack_allocs);
  526. end;
  527. procedure tcg386.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  528. begin
  529. { Nothing to release }
  530. end;
  531. procedure tcg386.g_exception_reason_save(list : TAsmList; const href : treference);
  532. begin
  533. if not paramanager.use_fixed_stack then
  534. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_INT],NR_FUNCTION_RESULT_REG))
  535. else
  536. inherited g_exception_reason_save(list,href);
  537. end;
  538. procedure tcg386.g_exception_reason_save_const(list : TAsmList;const href : treference; a: tcgint);
  539. begin
  540. if not paramanager.use_fixed_stack then
  541. list.concat(Taicpu.op_const(A_PUSH,tcgsize2opsize[OS_INT],a))
  542. else
  543. inherited g_exception_reason_save_const(list,href,a);
  544. end;
  545. procedure tcg386.g_exception_reason_load(list : TAsmList; const href : treference);
  546. begin
  547. if not paramanager.use_fixed_stack then
  548. begin
  549. a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  550. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[OS_INT],NR_FUNCTION_RESULT_REG))
  551. end
  552. else
  553. inherited g_exception_reason_load(list,href);
  554. end;
  555. procedure tcg386.g_maybe_got_init(list: TAsmList);
  556. var
  557. notdarwin: boolean;
  558. begin
  559. { allocate PIC register }
  560. if (cs_create_pic in current_settings.moduleswitches) and
  561. (tf_pic_uses_got in target_info.flags) and
  562. (pi_needs_got in current_procinfo.flags) then
  563. begin
  564. notdarwin:=not(target_info.system in [system_i386_darwin,system_i386_iphonesim]);
  565. { on darwin, the got register is virtual (and allocated earlier
  566. already) }
  567. if notdarwin then
  568. { ecx could be used in leaf procedures that don't use ecx to pass
  569. aparameter }
  570. current_procinfo.got:=NR_EBX;
  571. if notdarwin { needs testing before it can be enabled for non-darwin platforms
  572. and
  573. (current_settings.optimizecputype in [cpu_Pentium2,cpu_Pentium3,cpu_Pentium4]) } then
  574. begin
  575. current_module.requires_ebx_pic_helper:=true;
  576. a_call_name_static(list,'fpc_geteipasebx');
  577. end
  578. else
  579. begin
  580. { call/pop is faster than call/ret/mov on Core Solo and later
  581. according to Apple's benchmarking -- and all Intel Macs
  582. have at least a Core Solo (furthermore, the i386 - Pentium 1
  583. don't have a return stack buffer) }
  584. a_call_name_static(list,current_procinfo.CurrGOTLabel.name);
  585. a_label(list,current_procinfo.CurrGotLabel);
  586. list.concat(taicpu.op_reg(A_POP,S_L,current_procinfo.got))
  587. end;
  588. if notdarwin then
  589. begin
  590. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),0,NR_PIC_OFFSET_REG));
  591. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  592. end;
  593. end;
  594. end;
  595. procedure tcg386.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  596. {
  597. possible calling conventions:
  598. default stdcall cdecl pascal register
  599. default(0): OK OK OK OK OK
  600. virtual(1): OK OK OK OK OK(2 or 1)
  601. (0):
  602. set self parameter to correct value
  603. jmp mangledname
  604. (1): The wrapper code use %ecx to reach the virtual method address
  605. set self to correct value
  606. move self,%eax
  607. mov 0(%eax),%ecx ; load vmt
  608. jmp vmtoffs(%ecx) ; method offs
  609. (2): Virtual use values pushed on stack to reach the method address
  610. so the following code be generated:
  611. set self to correct value
  612. push %ebx ; allocate space for function address
  613. push %eax
  614. mov self,%eax
  615. mov 0(%eax),%eax ; load vmt
  616. mov vmtoffs(%eax),eax ; method offs
  617. mov %eax,4(%esp)
  618. pop %eax
  619. ret 0; jmp the address
  620. }
  621. { returns whether ECX is used (either as a parameter or is nonvolatile and shouldn't be changed) }
  622. function is_ecx_used: boolean;
  623. var
  624. i: Integer;
  625. hp: tparavarsym;
  626. paraloc: PCGParaLocation;
  627. begin
  628. if not (RS_ECX in paramanager.get_volatile_registers_int(procdef.proccalloption)) then
  629. exit(true);
  630. for i:=0 to procdef.paras.count-1 do
  631. begin
  632. hp:=tparavarsym(procdef.paras[i]);
  633. procdef.init_paraloc_info(calleeside);
  634. paraloc:=hp.paraloc[calleeside].Location;
  635. while paraloc<>nil do
  636. begin
  637. if (paraloc^.Loc=LOC_REGISTER) and (getsupreg(paraloc^.register)=RS_ECX) then
  638. exit(true);
  639. paraloc:=paraloc^.Next;
  640. end;
  641. end;
  642. Result:=false;
  643. end;
  644. procedure getselftoeax(offs: longint);
  645. var
  646. href : treference;
  647. selfoffsetfromsp : longint;
  648. begin
  649. { mov offset(%esp),%eax }
  650. if (procdef.proccalloption<>pocall_register) then
  651. begin
  652. { framepointer is pushed for nested procs }
  653. if procdef.parast.symtablelevel>normal_function_level then
  654. selfoffsetfromsp:=2*sizeof(aint)
  655. else
  656. selfoffsetfromsp:=sizeof(aint);
  657. reference_reset_base(href,NR_ESP,selfoffsetfromsp+offs,4);
  658. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  659. end;
  660. end;
  661. procedure loadvmtto(reg: tregister);
  662. var
  663. href : treference;
  664. begin
  665. { mov 0(%eax),%reg ; load vmt}
  666. reference_reset_base(href,NR_EAX,0,4);
  667. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,reg);
  668. end;
  669. procedure op_onregmethodaddr(op: TAsmOp; reg: tregister);
  670. var
  671. href : treference;
  672. begin
  673. if (procdef.extnumber=$ffff) then
  674. Internalerror(200006139);
  675. { call/jmp vmtoffs(%reg) ; method offs }
  676. reference_reset_base(href,reg,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  677. list.concat(taicpu.op_ref(op,S_L,href));
  678. end;
  679. procedure loadmethodoffstoeax;
  680. var
  681. href : treference;
  682. begin
  683. if (procdef.extnumber=$ffff) then
  684. Internalerror(200006139);
  685. { mov vmtoffs(%eax),%eax ; method offs }
  686. reference_reset_base(href,NR_EAX,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  687. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  688. end;
  689. var
  690. lab : tasmsymbol;
  691. make_global : boolean;
  692. href : treference;
  693. begin
  694. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  695. Internalerror(200006137);
  696. if not assigned(procdef.struct) or
  697. (procdef.procoptions*[po_classmethod, po_staticmethod,
  698. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  699. Internalerror(200006138);
  700. if procdef.owner.symtabletype<>ObjectSymtable then
  701. Internalerror(200109191);
  702. make_global:=false;
  703. if (not current_module.is_unit) or
  704. create_smartlink or
  705. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  706. make_global:=true;
  707. if make_global then
  708. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  709. else
  710. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  711. { set param1 interface to self }
  712. g_adjust_self_value(list,procdef,ioffset);
  713. if (po_virtualmethod in procdef.procoptions) and
  714. not is_objectpascal_helper(procdef.struct) then
  715. begin
  716. if (procdef.proccalloption=pocall_register) and is_ecx_used then
  717. begin
  718. { case 2 }
  719. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EBX)); { allocate space for address}
  720. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  721. getselftoeax(8);
  722. loadvmtto(NR_EAX);
  723. loadmethodoffstoeax;
  724. { mov %eax,4(%esp) }
  725. reference_reset_base(href,NR_ESP,4,4);
  726. list.concat(taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  727. { pop %eax }
  728. list.concat(taicpu.op_reg(A_POP,S_L,NR_EAX));
  729. { ret ; jump to the address }
  730. list.concat(taicpu.op_none(A_RET,S_L));
  731. end
  732. else
  733. begin
  734. { case 1 }
  735. getselftoeax(0);
  736. loadvmtto(NR_ECX);
  737. op_onregmethodaddr(A_JMP,NR_ECX);
  738. end;
  739. end
  740. { case 0 }
  741. else
  742. begin
  743. if (target_info.system <> system_i386_darwin) then
  744. begin
  745. lab:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  746. list.concat(taicpu.op_sym(A_JMP,S_NO,lab))
  747. end
  748. else
  749. list.concat(taicpu.op_sym(A_JMP,S_NO,get_darwin_call_stub(procdef.mangledname,false)))
  750. end;
  751. List.concat(Tai_symbol_end.Createname(labelname));
  752. end;
  753. { ************* 64bit operations ************ }
  754. procedure tcg64f386.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  755. begin
  756. case op of
  757. OP_ADD :
  758. begin
  759. op1:=A_ADD;
  760. op2:=A_ADC;
  761. end;
  762. OP_SUB :
  763. begin
  764. op1:=A_SUB;
  765. op2:=A_SBB;
  766. end;
  767. OP_XOR :
  768. begin
  769. op1:=A_XOR;
  770. op2:=A_XOR;
  771. end;
  772. OP_OR :
  773. begin
  774. op1:=A_OR;
  775. op2:=A_OR;
  776. end;
  777. OP_AND :
  778. begin
  779. op1:=A_AND;
  780. op2:=A_AND;
  781. end;
  782. else
  783. internalerror(200203241);
  784. end;
  785. end;
  786. procedure tcg64f386.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  787. var
  788. op1,op2 : TAsmOp;
  789. tempref : treference;
  790. begin
  791. if not(op in [OP_NEG,OP_NOT]) then
  792. begin
  793. get_64bit_ops(op,op1,op2);
  794. tempref:=ref;
  795. tcgx86(cg).make_simple_ref(list,tempref);
  796. list.concat(taicpu.op_ref_reg(op1,S_L,tempref,reg.reglo));
  797. inc(tempref.offset,4);
  798. list.concat(taicpu.op_ref_reg(op2,S_L,tempref,reg.reghi));
  799. end
  800. else
  801. begin
  802. a_load64_ref_reg(list,ref,reg);
  803. a_op64_reg_reg(list,op,size,reg,reg);
  804. end;
  805. end;
  806. procedure tcg64f386.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  807. var
  808. op1,op2 : TAsmOp;
  809. begin
  810. case op of
  811. OP_NEG :
  812. begin
  813. if (regsrc.reglo<>regdst.reglo) then
  814. a_load64_reg_reg(list,regsrc,regdst);
  815. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  816. list.concat(taicpu.op_reg(A_NEG,S_L,regdst.reglo));
  817. list.concat(taicpu.op_const_reg(A_SBB,S_L,-1,regdst.reghi));
  818. exit;
  819. end;
  820. OP_NOT :
  821. begin
  822. if (regsrc.reglo<>regdst.reglo) then
  823. a_load64_reg_reg(list,regsrc,regdst);
  824. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  825. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reglo));
  826. exit;
  827. end;
  828. end;
  829. get_64bit_ops(op,op1,op2);
  830. list.concat(taicpu.op_reg_reg(op1,S_L,regsrc.reglo,regdst.reglo));
  831. list.concat(taicpu.op_reg_reg(op2,S_L,regsrc.reghi,regdst.reghi));
  832. end;
  833. procedure tcg64f386.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  834. var
  835. op1,op2 : TAsmOp;
  836. begin
  837. case op of
  838. OP_AND,OP_OR,OP_XOR:
  839. begin
  840. cg.a_op_const_reg(list,op,OS_32,tcgint(lo(value)),reg.reglo);
  841. cg.a_op_const_reg(list,op,OS_32,tcgint(hi(value)),reg.reghi);
  842. end;
  843. OP_ADD, OP_SUB:
  844. begin
  845. // can't use a_op_const_ref because this may use dec/inc
  846. get_64bit_ops(op,op1,op2);
  847. list.concat(taicpu.op_const_reg(op1,S_L,aint(lo(value)),reg.reglo));
  848. list.concat(taicpu.op_const_reg(op2,S_L,aint(hi(value)),reg.reghi));
  849. end;
  850. else
  851. internalerror(200204021);
  852. end;
  853. end;
  854. procedure tcg64f386.a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);
  855. var
  856. op1,op2 : TAsmOp;
  857. tempref : treference;
  858. begin
  859. tempref:=ref;
  860. tcgx86(cg).make_simple_ref(list,tempref);
  861. case op of
  862. OP_AND,OP_OR,OP_XOR:
  863. begin
  864. cg.a_op_const_ref(list,op,OS_32,tcgint(lo(value)),tempref);
  865. inc(tempref.offset,4);
  866. cg.a_op_const_ref(list,op,OS_32,tcgint(hi(value)),tempref);
  867. end;
  868. OP_ADD, OP_SUB:
  869. begin
  870. get_64bit_ops(op,op1,op2);
  871. // can't use a_op_const_ref because this may use dec/inc
  872. list.concat(taicpu.op_const_ref(op1,S_L,aint(lo(value)),tempref));
  873. inc(tempref.offset,4);
  874. list.concat(taicpu.op_const_ref(op2,S_L,aint(hi(value)),tempref));
  875. end;
  876. else
  877. internalerror(200204022);
  878. end;
  879. end;
  880. procedure create_codegen;
  881. begin
  882. cg := tcg386.create;
  883. cg64 := tcg64f386.create;
  884. end;
  885. end.