cgcpu.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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_maybe_got_init(list: TAsmList); override;
  40. end;
  41. tcg64f386 = class(tcg64f32)
  42. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  43. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  44. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  45. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);override;
  46. private
  47. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  48. end;
  49. procedure create_codegen;
  50. implementation
  51. uses
  52. globals,verbose,systems,cutils,
  53. paramgr,procinfo,fmodule,
  54. rgcpu,rgx86,cpuinfo;
  55. function use_push(const cgpara:tcgpara):boolean;
  56. begin
  57. result:=(not paramanager.use_fixed_stack) and
  58. assigned(cgpara.location) and
  59. (cgpara.location^.loc=LOC_REFERENCE) and
  60. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  61. end;
  62. procedure tcg386.init_register_allocators;
  63. begin
  64. inherited init_register_allocators;
  65. if not(target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  66. (cs_create_pic in current_settings.moduleswitches) then
  67. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP])
  68. else
  69. if (cs_useebp in current_settings.optimizerswitches) and assigned(current_procinfo) and (current_procinfo.framepointer<>NR_EBP) then
  70. 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,[])
  71. else
  72. 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]);
  73. 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,[]);
  74. 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,[]);
  75. rgfpu:=Trgx86fpu.create;
  76. end;
  77. procedure tcg386.do_register_allocation(list:TAsmList;headertai:tai);
  78. begin
  79. if (pi_needs_got in current_procinfo.flags) then
  80. begin
  81. if getsupreg(current_procinfo.got) < first_int_imreg then
  82. include(rg[R_INTREGISTER].used_in_proc,getsupreg(current_procinfo.got));
  83. end;
  84. inherited do_register_allocation(list,headertai);
  85. end;
  86. procedure tcg386.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  87. var
  88. pushsize : tcgsize;
  89. begin
  90. check_register_size(size,r);
  91. if use_push(cgpara) then
  92. begin
  93. cgpara.check_simple_location;
  94. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  95. pushsize:=cgpara.location^.size
  96. else
  97. pushsize:=int_cgsize(cgpara.alignment);
  98. list.concat(taicpu.op_reg(A_PUSH,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize)));
  99. end
  100. else
  101. inherited a_load_reg_cgpara(list,size,r,cgpara);
  102. end;
  103. procedure tcg386.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  104. var
  105. pushsize : tcgsize;
  106. begin
  107. if use_push(cgpara) then
  108. begin
  109. cgpara.check_simple_location;
  110. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  111. pushsize:=cgpara.location^.size
  112. else
  113. pushsize:=int_cgsize(cgpara.alignment);
  114. list.concat(taicpu.op_const(A_PUSH,tcgsize2opsize[pushsize],a));
  115. end
  116. else
  117. inherited a_load_const_cgpara(list,size,a,cgpara);
  118. end;
  119. procedure tcg386.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  120. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  121. var
  122. pushsize : tcgsize;
  123. opsize : topsize;
  124. tmpreg : tregister;
  125. href : treference;
  126. begin
  127. if not assigned(paraloc) then
  128. exit;
  129. if (paraloc^.loc<>LOC_REFERENCE) or
  130. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  131. (tcgsize2size[paraloc^.size]>sizeof(aint)) then
  132. internalerror(200501162);
  133. { Pushes are needed in reverse order, add the size of the
  134. current location to the offset where to load from. This
  135. prevents wrong calculations for the last location when
  136. the size is not a power of 2 }
  137. if assigned(paraloc^.next) then
  138. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  139. { Push the data starting at ofs }
  140. href:=r;
  141. inc(href.offset,ofs);
  142. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  143. pushsize:=paraloc^.size
  144. else
  145. pushsize:=int_cgsize(cgpara.alignment);
  146. opsize:=TCgsize2opsize[pushsize];
  147. { for go32v2 we obtain OS_F32,
  148. but pushs is not valid, we need pushl }
  149. if opsize=S_FS then
  150. opsize:=S_L;
  151. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  152. begin
  153. tmpreg:=getintregister(list,pushsize);
  154. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  155. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  156. end
  157. else
  158. begin
  159. make_simple_ref(list,href);
  160. list.concat(taicpu.op_ref(A_PUSH,opsize,href));
  161. end;
  162. end;
  163. var
  164. len : tcgint;
  165. href : treference;
  166. begin
  167. { cgpara.size=OS_NO requires a copy on the stack }
  168. if use_push(cgpara) then
  169. begin
  170. { Record copy? }
  171. if (cgpara.size=OS_NO) or (size=OS_NO) then
  172. begin
  173. cgpara.check_simple_location;
  174. len:=align(cgpara.intsize,cgpara.alignment);
  175. g_stackpointer_alloc(list,len);
  176. reference_reset_base(href,NR_STACK_POINTER_REG,0,4);
  177. g_concatcopy(list,r,href,len);
  178. end
  179. else
  180. begin
  181. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  182. internalerror(200501161);
  183. if (cgpara.size=OS_F64) then
  184. begin
  185. href:=r;
  186. make_simple_ref(list,href);
  187. inc(href.offset,4);
  188. list.concat(taicpu.op_ref(A_PUSH,S_L,href));
  189. dec(href.offset,4);
  190. list.concat(taicpu.op_ref(A_PUSH,S_L,href));
  191. end
  192. else
  193. { We need to push the data in reverse order,
  194. therefor we use a recursive algorithm }
  195. pushdata(cgpara.location,0);
  196. end
  197. end
  198. else
  199. inherited a_load_ref_cgpara(list,size,r,cgpara);
  200. end;
  201. procedure tcg386.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  202. var
  203. tmpreg : tregister;
  204. opsize : topsize;
  205. tmpref : treference;
  206. begin
  207. with r do
  208. begin
  209. if use_push(cgpara) then
  210. begin
  211. cgpara.check_simple_location;
  212. opsize:=tcgsize2opsize[OS_ADDR];
  213. if (segment=NR_NO) and (base=NR_NO) and (index=NR_NO) then
  214. begin
  215. if assigned(symbol) then
  216. begin
  217. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  218. ((r.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  219. (cs_create_pic in current_settings.moduleswitches)) then
  220. begin
  221. tmpreg:=getaddressregister(list);
  222. a_loadaddr_ref_reg(list,r,tmpreg);
  223. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  224. end
  225. else if cs_create_pic in current_settings.moduleswitches then
  226. begin
  227. if offset<>0 then
  228. begin
  229. tmpreg:=getaddressregister(list);
  230. a_loadaddr_ref_reg(list,r,tmpreg);
  231. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  232. end
  233. else
  234. begin
  235. reference_reset_symbol(tmpref,r.symbol,0,r.alignment);
  236. tmpref.refaddr:=addr_pic;
  237. tmpref.base:=current_procinfo.got;
  238. {$ifdef EXTDEBUG}
  239. if not (pi_needs_got in current_procinfo.flags) then
  240. Comment(V_warning,'pi_needs_got not included');
  241. {$endif EXTDEBUG}
  242. include(current_procinfo.flags,pi_needs_got);
  243. list.concat(taicpu.op_ref(A_PUSH,S_L,tmpref));
  244. end
  245. end
  246. else
  247. list.concat(Taicpu.Op_sym_ofs(A_PUSH,opsize,symbol,offset));
  248. end
  249. else
  250. list.concat(Taicpu.Op_const(A_PUSH,opsize,offset));
  251. end
  252. else if (segment=NR_NO) and (base=NR_NO) and (index<>NR_NO) and
  253. (offset=0) and (scalefactor=0) and (symbol=nil) then
  254. list.concat(Taicpu.Op_reg(A_PUSH,opsize,index))
  255. else if (segment=NR_NO) and (base<>NR_NO) and (index=NR_NO) and
  256. (offset=0) and (symbol=nil) then
  257. list.concat(Taicpu.Op_reg(A_PUSH,opsize,base))
  258. else
  259. begin
  260. tmpreg:=getaddressregister(list);
  261. a_loadaddr_ref_reg(list,r,tmpreg);
  262. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  263. end;
  264. end
  265. else
  266. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  267. end;
  268. end;
  269. procedure tcg386.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  270. procedure increase_sp(a : tcgint);
  271. var
  272. href : treference;
  273. begin
  274. reference_reset_base(href,NR_STACK_POINTER_REG,a,0);
  275. { normally, lea is a better choice than an add }
  276. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  277. end;
  278. begin
  279. { Release PIC register }
  280. if (cs_create_pic in current_settings.moduleswitches) and
  281. (tf_pic_uses_got in target_info.flags) and
  282. (pi_needs_got in current_procinfo.flags) and
  283. not(target_info.system in systems_darwin) then
  284. list.concat(tai_regalloc.dealloc(NR_PIC_OFFSET_REG,nil));
  285. { MMX needs to call EMMS }
  286. if assigned(rg[R_MMXREGISTER]) and
  287. (rg[R_MMXREGISTER].uses_registers) then
  288. list.concat(Taicpu.op_none(A_EMMS,S_NO));
  289. { remove stackframe }
  290. if not nostackframe then
  291. begin
  292. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  293. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  294. begin
  295. if current_procinfo.final_localsize<>0 then
  296. increase_sp(current_procinfo.final_localsize);
  297. if (not paramanager.use_fixed_stack) then
  298. internal_restore_regs(list,true);
  299. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  300. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  301. end
  302. else
  303. begin
  304. if (not paramanager.use_fixed_stack) then
  305. internal_restore_regs(list,not (pi_has_stack_allocs in current_procinfo.flags));
  306. generate_leave(list);
  307. end;
  308. list.concat(tai_regalloc.dealloc(current_procinfo.framepointer,nil));
  309. end;
  310. { return from proc }
  311. if (po_interrupt in current_procinfo.procdef.procoptions) and
  312. { this messes up stack alignment }
  313. (target_info.stackalign=4) then
  314. begin
  315. if assigned(current_procinfo.procdef.funcretloc[calleeside].location) and
  316. (current_procinfo.procdef.funcretloc[calleeside].location^.loc=LOC_REGISTER) then
  317. begin
  318. if (getsupreg(current_procinfo.procdef.funcretloc[calleeside].location^.register)=RS_EAX) then
  319. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  320. else
  321. internalerror(2010053001);
  322. end
  323. else
  324. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EAX));
  325. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EBX));
  326. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ECX));
  327. if (current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64]) and
  328. assigned(current_procinfo.procdef.funcretloc[calleeside].location) and
  329. assigned(current_procinfo.procdef.funcretloc[calleeside].location^.next) and
  330. (current_procinfo.procdef.funcretloc[calleeside].location^.next^.loc=LOC_REGISTER) then
  331. begin
  332. if (getsupreg(current_procinfo.procdef.funcretloc[calleeside].location^.next^.register)=RS_EDX) then
  333. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  334. else
  335. internalerror(2010053002);
  336. end
  337. else
  338. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  339. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ESI));
  340. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDI));
  341. { .... also the segment registers }
  342. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  343. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_ES));
  344. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_FS));
  345. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_GS));
  346. { this restores the flags }
  347. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  348. end
  349. { Routines with the poclearstack flag set use only a ret }
  350. else if (current_procinfo.procdef.proccalloption in clearstack_pocalls) and
  351. (not paramanager.use_fixed_stack) then
  352. begin
  353. { complex return values are removed from stack in C code PM }
  354. { but not on win32 }
  355. { and not for safecall with hidden exceptions, because the result }
  356. { wich contains the exception is passed in EAX }
  357. if ((target_info.system <> system_i386_win32) or
  358. (target_info.abi=abi_old_win32_gnu)) and
  359. not ((current_procinfo.procdef.proccalloption = pocall_safecall) and
  360. (tf_safecall_exceptions in target_info.flags)) and
  361. paramanager.ret_in_param(current_procinfo.procdef.returndef,
  362. current_procinfo.procdef) then
  363. list.concat(Taicpu.Op_const(A_RET,S_W,sizeof(aint)))
  364. else
  365. list.concat(Taicpu.Op_none(A_RET,S_NO));
  366. end
  367. { ... also routines with parasize=0 }
  368. else if (parasize=0) then
  369. list.concat(Taicpu.Op_none(A_RET,S_NO))
  370. else
  371. begin
  372. { parameters are limited to 65535 bytes because ret allows only imm16 }
  373. if (parasize>65535) then
  374. CGMessage(cg_e_parasize_too_big);
  375. list.concat(Taicpu.Op_const(A_RET,S_W,parasize));
  376. end;
  377. end;
  378. procedure tcg386.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  379. var
  380. power : longint;
  381. opsize : topsize;
  382. {$ifndef __NOWINPECOFF__}
  383. again,ok : tasmlabel;
  384. {$endif}
  385. begin
  386. { get stack space }
  387. getcpuregister(list,NR_EDI);
  388. a_load_loc_reg(list,OS_INT,lenloc,NR_EDI);
  389. list.concat(Taicpu.op_reg(A_INC,S_L,NR_EDI));
  390. { Now EDI contains (high+1). }
  391. { special case handling for elesize=8, 4 and 2:
  392. set ECX = (high+1) instead of ECX = (high+1)*elesize.
  393. In the case of elesize=4 and 2, this allows us to avoid the SHR later.
  394. In the case of elesize=8, we can later use a SHL ECX, 1 instead of
  395. SHR ECX, 2 which is one byte shorter. }
  396. if (elesize=8) or (elesize=4) or (elesize=2) then
  397. begin
  398. { Now EDI contains (high+1). Copy it to ECX for later use. }
  399. getcpuregister(list,NR_ECX);
  400. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,NR_EDI,NR_ECX));
  401. end;
  402. { EDI := EDI * elesize }
  403. if (elesize<>1) then
  404. begin
  405. if ispowerof2(elesize, power) then
  406. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,NR_EDI))
  407. else
  408. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,NR_EDI));
  409. end;
  410. if (elesize<>8) and (elesize<>4) and (elesize<>2) then
  411. begin
  412. { Now EDI contains (high+1)*elesize. Copy it to ECX for later use. }
  413. getcpuregister(list,NR_ECX);
  414. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,NR_EDI,NR_ECX));
  415. end;
  416. {$ifndef __NOWINPECOFF__}
  417. { windows guards only a few pages for stack growing, }
  418. { so we have to access every page first }
  419. if target_info.system=system_i386_win32 then
  420. begin
  421. current_asmdata.getjumplabel(again);
  422. current_asmdata.getjumplabel(ok);
  423. a_label(list,again);
  424. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,NR_EDI));
  425. a_jmp_cond(list,OC_B,ok);
  426. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  427. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  428. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,NR_EDI));
  429. a_jmp_always(list,again);
  430. a_label(list,ok);
  431. end;
  432. {$endif __NOWINPECOFF__}
  433. { If we were probing pages, EDI=(size mod pagesize) and ESP is decremented
  434. by (size div pagesize)*pagesize, otherwise EDI=size.
  435. Either way, subtracting EDI from ESP will set ESP to desired final value. }
  436. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,NR_EDI,NR_ESP));
  437. { align stack on 4 bytes }
  438. list.concat(Taicpu.op_const_reg(A_AND,S_L,aint($fffffff4),NR_ESP));
  439. { load destination, don't use a_load_reg_reg, that will add a move instruction
  440. that can confuse the reg allocator }
  441. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,NR_ESP,NR_EDI));
  442. { Allocate ESI and load it with source }
  443. getcpuregister(list,NR_ESI);
  444. a_loadaddr_ref_reg(list,ref,NR_ESI);
  445. { calculate size }
  446. opsize:=S_B;
  447. if elesize=8 then
  448. begin
  449. opsize:=S_L;
  450. { ECX is number of qwords, convert to dwords }
  451. list.concat(Taicpu.op_const_reg(A_SHL,S_L,1,NR_ECX))
  452. end
  453. else if elesize=4 then
  454. begin
  455. opsize:=S_L;
  456. { ECX is already number of dwords, so no need to SHL/SHR }
  457. end
  458. else if elesize=2 then
  459. begin
  460. opsize:=S_W;
  461. { ECX is already number of words, so no need to SHL/SHR }
  462. end
  463. else
  464. if (elesize and 3)=0 then
  465. begin
  466. opsize:=S_L;
  467. { ECX is number of bytes, convert to dwords }
  468. list.concat(Taicpu.op_const_reg(A_SHR,S_L,2,NR_ECX))
  469. end
  470. else
  471. if (elesize and 1)=0 then
  472. begin
  473. opsize:=S_W;
  474. { ECX is number of bytes, convert to words }
  475. list.concat(Taicpu.op_const_reg(A_SHR,S_L,1,NR_ECX))
  476. end;
  477. if ts_cld in current_settings.targetswitches then
  478. list.concat(Taicpu.op_none(A_CLD,S_NO));
  479. list.concat(Taicpu.op_none(A_REP,S_NO));
  480. case opsize of
  481. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  482. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  483. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  484. end;
  485. ungetcpuregister(list,NR_EDI);
  486. ungetcpuregister(list,NR_ECX);
  487. ungetcpuregister(list,NR_ESI);
  488. { patch the new address, but don't use a_load_reg_reg, that will add a move instruction
  489. that can confuse the reg allocator }
  490. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,NR_ESP,destreg));
  491. include(current_procinfo.flags,pi_has_stack_allocs);
  492. end;
  493. procedure tcg386.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  494. begin
  495. { Nothing to release }
  496. end;
  497. procedure tcg386.g_maybe_got_init(list: TAsmList);
  498. var
  499. notdarwin: boolean;
  500. begin
  501. { allocate PIC register }
  502. if (cs_create_pic in current_settings.moduleswitches) and
  503. (tf_pic_uses_got in target_info.flags) and
  504. (pi_needs_got in current_procinfo.flags) then
  505. begin
  506. notdarwin:=not(target_info.system in [system_i386_darwin,system_i386_iphonesim]);
  507. { on darwin, the got register is virtual (and allocated earlier
  508. already) }
  509. if notdarwin then
  510. { ecx could be used in leaf procedures that don't use ecx to pass
  511. aparameter }
  512. current_procinfo.got:=NR_EBX;
  513. if notdarwin { needs testing before it can be enabled for non-darwin platforms
  514. and
  515. (current_settings.optimizecputype in [cpu_Pentium2,cpu_Pentium3,cpu_Pentium4]) } then
  516. begin
  517. current_module.requires_ebx_pic_helper:=true;
  518. a_call_name_static(list,'fpc_geteipasebx');
  519. end
  520. else
  521. begin
  522. { call/pop is faster than call/ret/mov on Core Solo and later
  523. according to Apple's benchmarking -- and all Intel Macs
  524. have at least a Core Solo (furthermore, the i386 - Pentium 1
  525. don't have a return stack buffer) }
  526. a_call_name_static(list,current_procinfo.CurrGOTLabel.name);
  527. a_label(list,current_procinfo.CurrGotLabel);
  528. list.concat(taicpu.op_reg(A_POP,S_L,current_procinfo.got))
  529. end;
  530. if notdarwin then
  531. begin
  532. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),0,NR_PIC_OFFSET_REG));
  533. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  534. end;
  535. end;
  536. end;
  537. { ************* 64bit operations ************ }
  538. procedure tcg64f386.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  539. begin
  540. case op of
  541. OP_ADD :
  542. begin
  543. op1:=A_ADD;
  544. op2:=A_ADC;
  545. end;
  546. OP_SUB :
  547. begin
  548. op1:=A_SUB;
  549. op2:=A_SBB;
  550. end;
  551. OP_XOR :
  552. begin
  553. op1:=A_XOR;
  554. op2:=A_XOR;
  555. end;
  556. OP_OR :
  557. begin
  558. op1:=A_OR;
  559. op2:=A_OR;
  560. end;
  561. OP_AND :
  562. begin
  563. op1:=A_AND;
  564. op2:=A_AND;
  565. end;
  566. else
  567. internalerror(200203241);
  568. end;
  569. end;
  570. procedure tcg64f386.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  571. var
  572. op1,op2 : TAsmOp;
  573. tempref : treference;
  574. begin
  575. if not(op in [OP_NEG,OP_NOT]) then
  576. begin
  577. get_64bit_ops(op,op1,op2);
  578. tempref:=ref;
  579. tcgx86(cg).make_simple_ref(list,tempref);
  580. list.concat(taicpu.op_ref_reg(op1,S_L,tempref,reg.reglo));
  581. inc(tempref.offset,4);
  582. list.concat(taicpu.op_ref_reg(op2,S_L,tempref,reg.reghi));
  583. end
  584. else
  585. begin
  586. a_load64_ref_reg(list,ref,reg);
  587. a_op64_reg_reg(list,op,size,reg,reg);
  588. end;
  589. end;
  590. procedure tcg64f386.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  591. var
  592. op1,op2 : TAsmOp;
  593. begin
  594. case op of
  595. OP_NEG :
  596. begin
  597. if (regsrc.reglo<>regdst.reglo) then
  598. a_load64_reg_reg(list,regsrc,regdst);
  599. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  600. list.concat(taicpu.op_reg(A_NEG,S_L,regdst.reglo));
  601. list.concat(taicpu.op_const_reg(A_SBB,S_L,-1,regdst.reghi));
  602. exit;
  603. end;
  604. OP_NOT :
  605. begin
  606. if (regsrc.reglo<>regdst.reglo) then
  607. a_load64_reg_reg(list,regsrc,regdst);
  608. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  609. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reglo));
  610. exit;
  611. end;
  612. end;
  613. get_64bit_ops(op,op1,op2);
  614. list.concat(taicpu.op_reg_reg(op1,S_L,regsrc.reglo,regdst.reglo));
  615. list.concat(taicpu.op_reg_reg(op2,S_L,regsrc.reghi,regdst.reghi));
  616. end;
  617. procedure tcg64f386.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  618. var
  619. op1,op2 : TAsmOp;
  620. begin
  621. case op of
  622. OP_AND,OP_OR,OP_XOR:
  623. begin
  624. cg.a_op_const_reg(list,op,OS_32,tcgint(lo(value)),reg.reglo);
  625. cg.a_op_const_reg(list,op,OS_32,tcgint(hi(value)),reg.reghi);
  626. end;
  627. OP_ADD, OP_SUB:
  628. begin
  629. // can't use a_op_const_ref because this may use dec/inc
  630. get_64bit_ops(op,op1,op2);
  631. list.concat(taicpu.op_const_reg(op1,S_L,aint(lo(value)),reg.reglo));
  632. list.concat(taicpu.op_const_reg(op2,S_L,aint(hi(value)),reg.reghi));
  633. end;
  634. else
  635. internalerror(200204021);
  636. end;
  637. end;
  638. procedure tcg64f386.a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);
  639. var
  640. op1,op2 : TAsmOp;
  641. tempref : treference;
  642. begin
  643. tempref:=ref;
  644. tcgx86(cg).make_simple_ref(list,tempref);
  645. case op of
  646. OP_AND,OP_OR,OP_XOR:
  647. begin
  648. cg.a_op_const_ref(list,op,OS_32,tcgint(lo(value)),tempref);
  649. inc(tempref.offset,4);
  650. cg.a_op_const_ref(list,op,OS_32,tcgint(hi(value)),tempref);
  651. end;
  652. OP_ADD, OP_SUB:
  653. begin
  654. get_64bit_ops(op,op1,op2);
  655. // can't use a_op_const_ref because this may use dec/inc
  656. list.concat(taicpu.op_const_ref(op1,S_L,aint(lo(value)),tempref));
  657. inc(tempref.offset,4);
  658. list.concat(taicpu.op_const_ref(op2,S_L,aint(hi(value)),tempref));
  659. end;
  660. else
  661. internalerror(200204022);
  662. end;
  663. end;
  664. procedure create_codegen;
  665. begin
  666. cg := tcg386.create;
  667. cg64 := tcg64f386.create;
  668. end;
  669. end.