cgcpu.pas 37 KB

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