cgcpu.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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
  26. ;
  27. type
  28. { tcg8086 }
  29. tcg8086 = class(tcgx86)
  30. procedure init_register_allocators;override;
  31. procedure do_register_allocation(list:TAsmList;headertai:tai);override;
  32. function getintregister(list:TAsmList;size:Tcgsize):Tregister;override;
  33. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  34. { passing parameter using push instead of mov }
  35. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  36. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  37. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  38. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  39. { move instructions }
  40. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  41. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  42. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  43. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  44. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  45. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  46. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  47. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  48. procedure g_exception_reason_save(list : TAsmList; const href : treference);override;
  49. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);override;
  50. procedure g_exception_reason_load(list : TAsmList; const href : treference);override;
  51. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  52. procedure g_maybe_got_init(list: TAsmList); override;
  53. procedure get_32bit_ops(op: TOpCG; out op1,op2: TAsmOp);
  54. end;
  55. tcg64f386 = class(tcg64f32)
  56. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  57. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  58. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  59. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);override;
  60. private
  61. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  62. end;
  63. procedure create_codegen;
  64. implementation
  65. uses
  66. globals,verbose,systems,cutils,
  67. paramgr,procinfo,fmodule,
  68. rgcpu,rgx86,cpuinfo;
  69. function use_push(const cgpara:tcgpara):boolean;
  70. begin
  71. result:=(not paramanager.use_fixed_stack) and
  72. assigned(cgpara.location) and
  73. (cgpara.location^.loc=LOC_REFERENCE) and
  74. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  75. end;
  76. procedure tcg8086.init_register_allocators;
  77. begin
  78. inherited init_register_allocators;
  79. if not(target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  80. (cs_create_pic in current_settings.moduleswitches) then
  81. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_SI,RS_DI],first_int_imreg,[RS_BP])
  82. else
  83. if (cs_useebp in current_settings.optimizerswitches) and assigned(current_procinfo) and (current_procinfo.framepointer<>NR_BP) then
  84. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_BX,RS_SI,RS_DI,RS_BP],first_int_imreg,[])
  85. else
  86. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_BX,RS_SI,RS_DI],first_int_imreg,[RS_BP]);
  87. 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,[]);
  88. 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,[]);
  89. rgfpu:=Trgx86fpu.create;
  90. end;
  91. procedure tcg8086.do_register_allocation(list:TAsmList;headertai:tai);
  92. begin
  93. if (pi_needs_got in current_procinfo.flags) then
  94. begin
  95. if getsupreg(current_procinfo.got) < first_int_imreg then
  96. include(rg[R_INTREGISTER].used_in_proc,getsupreg(current_procinfo.got));
  97. end;
  98. inherited do_register_allocation(list,headertai);
  99. end;
  100. function tcg8086.getintregister(list: TAsmList; size: Tcgsize): Tregister;
  101. begin
  102. case size of
  103. OS_8, OS_S8,
  104. OS_16, OS_S16:
  105. Result := inherited getintregister(list, size);
  106. OS_32, OS_S32:
  107. begin
  108. Result:=inherited getintregister(list, OS_16);
  109. { ensure that the high register can be retrieved by
  110. GetNextReg
  111. }
  112. if inherited getintregister(list, OS_16)<>GetNextReg(Result) then
  113. internalerror(2013030202);
  114. end;
  115. else
  116. internalerror(2013030201);
  117. end;
  118. end;
  119. procedure tcg8086.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize;
  120. src, dst: TRegister);
  121. var
  122. op1, op2: TAsmOp;
  123. hl_skip, hl_loop_start: TAsmLabel;
  124. ai: taicpu;
  125. begin
  126. check_register_size(size,src);
  127. check_register_size(size,dst);
  128. if size in [OS_64, OS_S64] then
  129. internalerror(2013030902);
  130. if size in [OS_32, OS_S32] then
  131. begin
  132. case op of
  133. OP_NEG:
  134. begin
  135. if src<>dst then
  136. a_load_reg_reg(list,size,size,src,dst);
  137. list.concat(taicpu.op_reg(A_NOT, S_W, GetNextReg(dst)));
  138. list.concat(taicpu.op_reg(A_NEG, S_W, dst));
  139. list.concat(taicpu.op_const_reg(A_SBB, S_W,-1, GetNextReg(dst)));
  140. end;
  141. OP_NOT:
  142. begin
  143. if src<>dst then
  144. a_load_reg_reg(list,size,size,src,dst);
  145. list.concat(taicpu.op_reg(A_NOT, S_W, dst));
  146. list.concat(taicpu.op_reg(A_NOT, S_W, GetNextReg(dst)));
  147. end;
  148. OP_ADD,OP_SUB,OP_XOR,OP_OR,OP_AND:
  149. begin
  150. get_32bit_ops(op, op1, op2);
  151. list.concat(taicpu.op_reg_reg(op1, S_W, src, dst));
  152. list.concat(taicpu.op_reg_reg(op2, S_W, GetNextReg(src), GetNextReg(dst)));
  153. end;
  154. OP_SHR,OP_SHL,OP_SAR:
  155. begin
  156. getcpuregister(list,NR_CX);
  157. a_load_reg_reg(list,size,OS_16,src,NR_CX);
  158. list.concat(taicpu.op_const_reg(A_AND,S_W,$1f,NR_CX));
  159. current_asmdata.getjumplabel(hl_skip);
  160. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl_skip);
  161. ai.SetCondition(C_Z);
  162. ai.is_jmp:=true;
  163. list.concat(ai);
  164. current_asmdata.getjumplabel(hl_loop_start);
  165. a_label(list,hl_loop_start);
  166. case op of
  167. OP_SHR:
  168. begin
  169. list.concat(taicpu.op_const_reg(A_SHR,S_W,1,GetNextReg(dst)));
  170. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,dst));
  171. end;
  172. OP_SAR:
  173. begin
  174. list.concat(taicpu.op_const_reg(A_SAR,S_W,1,GetNextReg(dst)));
  175. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,dst));
  176. end;
  177. OP_SHL:
  178. begin
  179. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,dst));
  180. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(dst)));
  181. end;
  182. else
  183. internalerror(2013030903);
  184. end;
  185. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  186. ai.is_jmp:=true;
  187. list.concat(ai);
  188. a_label(list,hl_skip);
  189. ungetcpuregister(list,NR_CX);
  190. end;
  191. else
  192. internalerror(2013030901);
  193. end;
  194. end
  195. else
  196. inherited a_op_reg_reg(list, Op, size, src, dst);
  197. end;
  198. procedure tcg8086.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  199. var
  200. pushsize : tcgsize;
  201. begin
  202. check_register_size(size,r);
  203. if use_push(cgpara) then
  204. begin
  205. cgpara.check_simple_location;
  206. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  207. pushsize:=cgpara.location^.size
  208. else
  209. pushsize:=int_cgsize(cgpara.alignment);
  210. list.concat(taicpu.op_reg(A_PUSH,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize)));
  211. end
  212. else
  213. inherited a_load_reg_cgpara(list,size,r,cgpara);
  214. end;
  215. procedure tcg8086.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  216. var
  217. pushsize : tcgsize;
  218. begin
  219. if use_push(cgpara) then
  220. begin
  221. cgpara.check_simple_location;
  222. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  223. pushsize:=cgpara.location^.size
  224. else
  225. pushsize:=int_cgsize(cgpara.alignment);
  226. list.concat(taicpu.op_const(A_PUSH,tcgsize2opsize[pushsize],a));
  227. end
  228. else
  229. inherited a_load_const_cgpara(list,size,a,cgpara);
  230. end;
  231. procedure tcg8086.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  232. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  233. var
  234. pushsize : tcgsize;
  235. opsize : topsize;
  236. tmpreg : tregister;
  237. href : treference;
  238. begin
  239. if not assigned(paraloc) then
  240. exit;
  241. if (paraloc^.loc<>LOC_REFERENCE) or
  242. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  243. (tcgsize2size[paraloc^.size]>sizeof(aint)) then
  244. internalerror(200501162);
  245. { Pushes are needed in reverse order, add the size of the
  246. current location to the offset where to load from. This
  247. prevents wrong calculations for the last location when
  248. the size is not a power of 2 }
  249. if assigned(paraloc^.next) then
  250. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  251. { Push the data starting at ofs }
  252. href:=r;
  253. inc(href.offset,ofs);
  254. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  255. pushsize:=paraloc^.size
  256. else
  257. pushsize:=int_cgsize(cgpara.alignment);
  258. opsize:=TCgsize2opsize[pushsize];
  259. { for go32v2 we obtain OS_F32,
  260. but pushs is not valid, we need pushl }
  261. if opsize=S_FS then
  262. opsize:=S_L;
  263. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  264. begin
  265. tmpreg:=getintregister(list,pushsize);
  266. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  267. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  268. end
  269. else
  270. begin
  271. make_simple_ref(list,href);
  272. list.concat(taicpu.op_ref(A_PUSH,opsize,href));
  273. end;
  274. end;
  275. var
  276. len : tcgint;
  277. href : treference;
  278. begin
  279. { cgpara.size=OS_NO requires a copy on the stack }
  280. if use_push(cgpara) then
  281. begin
  282. { Record copy? }
  283. if (cgpara.size in [OS_NO,OS_F64]) or (size=OS_NO) then
  284. begin
  285. cgpara.check_simple_location;
  286. len:=align(cgpara.intsize,cgpara.alignment);
  287. g_stackpointer_alloc(list,len);
  288. reference_reset_base(href,NR_STACK_POINTER_REG,0,4);
  289. g_concatcopy(list,r,href,len);
  290. end
  291. else
  292. begin
  293. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  294. internalerror(200501161);
  295. { We need to push the data in reverse order,
  296. therefor we use a recursive algorithm }
  297. pushdata(cgpara.location,0);
  298. end
  299. end
  300. else
  301. inherited a_load_ref_cgpara(list,size,r,cgpara);
  302. end;
  303. procedure tcg8086.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  304. var
  305. tmpreg : tregister;
  306. opsize : topsize;
  307. tmpref : treference;
  308. begin
  309. with r do
  310. begin
  311. if use_push(cgpara) then
  312. begin
  313. cgpara.check_simple_location;
  314. opsize:=tcgsize2opsize[OS_ADDR];
  315. if (segment=NR_NO) and (base=NR_NO) and (index=NR_NO) then
  316. begin
  317. if assigned(symbol) then
  318. begin
  319. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  320. ((r.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  321. (cs_create_pic in current_settings.moduleswitches)) then
  322. begin
  323. tmpreg:=getaddressregister(list);
  324. a_loadaddr_ref_reg(list,r,tmpreg);
  325. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  326. end
  327. else if cs_create_pic in current_settings.moduleswitches then
  328. begin
  329. if offset<>0 then
  330. begin
  331. tmpreg:=getaddressregister(list);
  332. a_loadaddr_ref_reg(list,r,tmpreg);
  333. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  334. end
  335. else
  336. begin
  337. reference_reset_symbol(tmpref,r.symbol,0,r.alignment);
  338. tmpref.refaddr:=addr_pic;
  339. tmpref.base:=current_procinfo.got;
  340. {$ifdef EXTDEBUG}
  341. if not (pi_needs_got in current_procinfo.flags) then
  342. Comment(V_warning,'pi_needs_got not included');
  343. {$endif EXTDEBUG}
  344. include(current_procinfo.flags,pi_needs_got);
  345. list.concat(taicpu.op_ref(A_PUSH,S_L,tmpref));
  346. end
  347. end
  348. else
  349. list.concat(Taicpu.Op_sym_ofs(A_PUSH,opsize,symbol,offset));
  350. end
  351. else
  352. list.concat(Taicpu.Op_const(A_PUSH,opsize,offset));
  353. end
  354. else if (segment=NR_NO) and (base=NR_NO) and (index<>NR_NO) and
  355. (offset=0) and (scalefactor=0) and (symbol=nil) then
  356. list.concat(Taicpu.Op_reg(A_PUSH,opsize,index))
  357. else if (segment=NR_NO) and (base<>NR_NO) and (index=NR_NO) and
  358. (offset=0) and (symbol=nil) then
  359. list.concat(Taicpu.Op_reg(A_PUSH,opsize,base))
  360. else
  361. begin
  362. tmpreg:=getaddressregister(list);
  363. a_loadaddr_ref_reg(list,r,tmpreg);
  364. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  365. end;
  366. end
  367. else
  368. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  369. end;
  370. end;
  371. procedure tcg8086.a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);
  372. begin
  373. check_register_size(tosize,reg);
  374. if tosize in [OS_S32,OS_32] then
  375. begin
  376. list.concat(taicpu.op_const_reg(A_MOV,S_W,longint(a and $ffff),reg));
  377. list.concat(taicpu.op_const_reg(A_MOV,S_W,longint(a shr 16),GetNextReg(reg)));
  378. end
  379. else
  380. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg));
  381. end;
  382. procedure tcg8086.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  383. var
  384. tmpref : treference;
  385. begin
  386. tmpref:=ref;
  387. make_simple_ref(list,tmpref);
  388. if tosize in [OS_S32,OS_32] then
  389. begin
  390. a_load_const_ref(list,OS_16,longint(a and $ffff),tmpref);
  391. inc(tmpref.offset,2);
  392. a_load_const_ref(list,OS_16,longint(a shr 16),tmpref);
  393. end
  394. else
  395. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  396. end;
  397. procedure tcg8086.a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);
  398. var
  399. tmpsize : tcgsize;
  400. tmpreg : tregister;
  401. tmpref : treference;
  402. begin
  403. tmpref:=ref;
  404. make_simple_ref(list,tmpref);
  405. check_register_size(fromsize,reg);
  406. case tosize of
  407. OS_8,OS_S8:
  408. if fromsize in [OS_8,OS_S8] then
  409. list.concat(taicpu.op_reg_ref(A_MOV, S_B, reg, tmpref))
  410. else
  411. internalerror(2013030310);
  412. OS_16,OS_S16:
  413. if fromsize in [OS_16,OS_S16] then
  414. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref))
  415. else
  416. internalerror(2013030312);
  417. OS_32,OS_S32:
  418. if fromsize in [OS_32,OS_S32] then
  419. begin
  420. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  421. inc(tmpref.offset, 2);
  422. list.concat(taicpu.op_reg_ref(A_MOV, S_W, GetNextReg(reg), tmpref));
  423. end
  424. else
  425. internalerror(2013030313);
  426. else
  427. internalerror(2013030311);
  428. end;
  429. end;
  430. procedure tcg8086.a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);
  431. procedure add_mov(instr: Taicpu);
  432. begin
  433. { Notify the register allocator that we have written a move instruction so
  434. it can try to eliminate it. }
  435. if (instr.oper[0]^.reg<>current_procinfo.framepointer) and (instr.oper[0]^.reg<>NR_STACK_POINTER_REG) then
  436. add_move_instruction(instr);
  437. list.concat(instr);
  438. end;
  439. var
  440. tmpref : treference;
  441. begin
  442. tmpref:=ref;
  443. make_simple_ref(list,tmpref);
  444. check_register_size(tosize,reg);
  445. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  446. internalerror(2011021307);
  447. { if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  448. fromsize:=tosize;}
  449. case tosize of
  450. OS_8,OS_S8:
  451. if fromsize in [OS_8,OS_S8] then
  452. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg))
  453. else
  454. internalerror(2013030210);
  455. OS_16,OS_S16:
  456. case fromsize of
  457. OS_8:
  458. begin
  459. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, reg));
  460. reg := makeregsize(list, reg, OS_8);
  461. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg));
  462. end;
  463. OS_S8:
  464. begin
  465. getcpuregister(list, NR_AX);
  466. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, NR_AL));
  467. list.concat(taicpu.op_none(A_CBW));
  468. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  469. ungetcpuregister(list, NR_AX);
  470. end;
  471. OS_16,OS_S16:
  472. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  473. else
  474. internalerror(2013030212);
  475. end;
  476. OS_32,OS_S32:
  477. case fromsize of
  478. OS_8:
  479. begin
  480. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg)));
  481. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, reg));
  482. reg := makeregsize(list, reg, OS_8);
  483. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg));
  484. end;
  485. OS_S8:
  486. begin
  487. getcpuregister(list, NR_AX);
  488. getcpuregister(list, NR_DX);
  489. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, NR_AL));
  490. list.concat(taicpu.op_none(A_CBW));
  491. list.concat(taicpu.op_none(A_CWD));
  492. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  493. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg)));
  494. ungetcpuregister(list, NR_AX);
  495. ungetcpuregister(list, NR_DX);
  496. end;
  497. OS_16:
  498. begin
  499. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  500. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg)));
  501. end;
  502. OS_S16:
  503. begin
  504. getcpuregister(list, NR_AX);
  505. getcpuregister(list, NR_DX);
  506. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, NR_AX));
  507. list.concat(taicpu.op_none(A_CWD));
  508. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  509. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg)));
  510. ungetcpuregister(list, NR_AX);
  511. ungetcpuregister(list, NR_DX);
  512. end;
  513. OS_32,OS_S32:
  514. begin
  515. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  516. inc(tmpref.offset, 2);
  517. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, GetNextReg(reg)));
  518. end;
  519. else
  520. internalerror(2013030213);
  521. end;
  522. else
  523. internalerror(2013030211);
  524. end;
  525. end;
  526. procedure tcg8086.a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);
  527. procedure add_mov(instr: Taicpu);
  528. begin
  529. { Notify the register allocator that we have written a move instruction so
  530. it can try to eliminate it. }
  531. if (instr.oper[0]^.reg<>current_procinfo.framepointer) and (instr.oper[0]^.reg<>NR_STACK_POINTER_REG) then
  532. add_move_instruction(instr);
  533. list.concat(instr);
  534. end;
  535. // var
  536. // op: tasmop;
  537. // s: topsize;
  538. // instr:Taicpu;
  539. begin
  540. check_register_size(fromsize,reg1);
  541. check_register_size(tosize,reg2);
  542. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  543. fromsize:=tosize;
  544. { if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  545. begin
  546. reg1:=makeregsize(list,reg1,tosize);
  547. s:=tcgsize2opsize[tosize];
  548. op:=A_MOV;
  549. end
  550. else
  551. sizes2load(fromsize,tosize,op,s);}
  552. if (reg1<>reg2) then
  553. begin
  554. case tosize of
  555. OS_8,OS_S8:
  556. if fromsize in [OS_8,OS_S8] then
  557. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2))
  558. else
  559. internalerror(2013030210);
  560. OS_16,OS_S16:
  561. case fromsize of
  562. OS_8:
  563. begin
  564. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, reg2));
  565. reg2 := makeregsize(list, reg2, OS_8);
  566. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2));
  567. end;
  568. OS_S8:
  569. begin
  570. getcpuregister(list, NR_AX);
  571. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, NR_AL));
  572. list.concat(taicpu.op_none(A_CBW));
  573. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  574. ungetcpuregister(list, NR_AX);
  575. end;
  576. OS_16,OS_S16:
  577. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  578. else
  579. internalerror(2013030212);
  580. end;
  581. OS_32,OS_S32:
  582. case fromsize of
  583. OS_8:
  584. begin
  585. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, GetNextReg(reg2)));
  586. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, reg2));
  587. reg2 := makeregsize(list, reg2, OS_8);
  588. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2));
  589. end;
  590. OS_S8:
  591. begin
  592. getcpuregister(list, NR_AX);
  593. getcpuregister(list, NR_DX);
  594. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, NR_AL));
  595. list.concat(taicpu.op_none(A_CBW));
  596. list.concat(taicpu.op_none(A_CWD));
  597. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  598. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg2)));
  599. ungetcpuregister(list, NR_AX);
  600. ungetcpuregister(list, NR_DX);
  601. end;
  602. OS_16:
  603. begin
  604. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  605. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg2)));
  606. end;
  607. OS_S16:
  608. begin
  609. getcpuregister(list, NR_AX);
  610. getcpuregister(list, NR_DX);
  611. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, NR_AX));
  612. list.concat(taicpu.op_none(A_CWD));
  613. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  614. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg2)));
  615. ungetcpuregister(list, NR_AX);
  616. ungetcpuregister(list, NR_DX);
  617. end;
  618. OS_32,OS_S32:
  619. begin
  620. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  621. add_mov(taicpu.op_reg_reg(A_MOV, S_W, GetNextReg(reg1), GetNextReg(reg2)));
  622. end;
  623. else
  624. internalerror(2013030213);
  625. end;
  626. else
  627. internalerror(2013030211);
  628. end;
  629. end;
  630. end;
  631. procedure tcg8086.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  632. var
  633. stacksize : longint;
  634. begin
  635. { MMX needs to call EMMS }
  636. if assigned(rg[R_MMXREGISTER]) and
  637. (rg[R_MMXREGISTER].uses_registers) then
  638. list.concat(Taicpu.op_none(A_EMMS,S_NO));
  639. { remove stackframe }
  640. if not nostackframe then
  641. begin
  642. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  643. begin
  644. stacksize:=current_procinfo.calc_stackframe_size;
  645. if (target_info.stackalign>4) and
  646. ((stacksize <> 0) or
  647. (pi_do_call in current_procinfo.flags) or
  648. { can't detect if a call in this case -> use nostackframe }
  649. { if you (think you) know what you are doing }
  650. (po_assembler in current_procinfo.procdef.procoptions)) then
  651. stacksize := align(stacksize+sizeof(aint),target_info.stackalign) - sizeof(aint);
  652. if (stacksize<>0) then
  653. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,stacksize,current_procinfo.framepointer);
  654. end
  655. else
  656. begin
  657. list.concat(Taicpu.op_reg_reg(A_MOV, S_W, NR_BP, NR_SP));
  658. list.concat(Taicpu.op_reg(A_POP, S_W, NR_BP));
  659. {todo: use LEAVE for 286+}
  660. {list.concat(Taicpu.op_none(A_LEAVE,S_NO));}
  661. end;
  662. list.concat(tai_regalloc.dealloc(current_procinfo.framepointer,nil));
  663. end;
  664. { return from proc }
  665. if (po_interrupt in current_procinfo.procdef.procoptions) and
  666. { this messes up stack alignment }
  667. (target_info.stackalign=4) then
  668. begin
  669. if assigned(current_procinfo.procdef.funcretloc[calleeside].location) and
  670. (current_procinfo.procdef.funcretloc[calleeside].location^.loc=LOC_REGISTER) then
  671. begin
  672. if (getsupreg(current_procinfo.procdef.funcretloc[calleeside].location^.register)=RS_EAX) then
  673. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  674. else
  675. internalerror(2010053001);
  676. end
  677. else
  678. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EAX));
  679. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EBX));
  680. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ECX));
  681. if (current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64]) and
  682. assigned(current_procinfo.procdef.funcretloc[calleeside].location) and
  683. assigned(current_procinfo.procdef.funcretloc[calleeside].location^.next) and
  684. (current_procinfo.procdef.funcretloc[calleeside].location^.next^.loc=LOC_REGISTER) then
  685. begin
  686. if (getsupreg(current_procinfo.procdef.funcretloc[calleeside].location^.next^.register)=RS_EDX) then
  687. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  688. else
  689. internalerror(2010053002);
  690. end
  691. else
  692. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  693. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ESI));
  694. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDI));
  695. { .... also the segment registers }
  696. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  697. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_ES));
  698. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_FS));
  699. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_GS));
  700. { this restores the flags }
  701. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  702. end
  703. { Routines with the poclearstack flag set use only a ret }
  704. else if (current_procinfo.procdef.proccalloption in clearstack_pocalls) and
  705. (not paramanager.use_fixed_stack) then
  706. begin
  707. { complex return values are removed from stack in C code PM }
  708. { but not on win32 }
  709. { and not for safecall with hidden exceptions, because the result }
  710. { wich contains the exception is passed in EAX }
  711. if (target_info.system <> system_i386_win32) and
  712. not ((current_procinfo.procdef.proccalloption = pocall_safecall) and
  713. (tf_safecall_exceptions in target_info.flags)) and
  714. paramanager.ret_in_param(current_procinfo.procdef.returndef,
  715. current_procinfo.procdef) then
  716. list.concat(Taicpu.Op_const(A_RET,S_W,sizeof(aint)))
  717. else
  718. list.concat(Taicpu.Op_none(A_RET,S_NO));
  719. end
  720. { ... also routines with parasize=0 }
  721. else if (parasize=0) then
  722. list.concat(Taicpu.Op_none(A_RET,S_NO))
  723. else
  724. begin
  725. { parameters are limited to 65535 bytes because ret allows only imm16 }
  726. if (parasize>65535) then
  727. CGMessage(cg_e_parasize_too_big);
  728. list.concat(Taicpu.Op_const(A_RET,S_W,parasize));
  729. end;
  730. end;
  731. procedure tcg8086.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  732. var
  733. power,len : longint;
  734. opsize : topsize;
  735. {$ifndef __NOWINPECOFF__}
  736. again,ok : tasmlabel;
  737. {$endif}
  738. begin
  739. { get stack space }
  740. getcpuregister(list,NR_EDI);
  741. a_load_loc_reg(list,OS_INT,lenloc,NR_EDI);
  742. list.concat(Taicpu.op_reg(A_INC,S_L,NR_EDI));
  743. { Now EDI contains (high+1). Copy it to ECX for later use. }
  744. getcpuregister(list,NR_ECX);
  745. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,NR_EDI,NR_ECX));
  746. if (elesize<>1) then
  747. begin
  748. if ispowerof2(elesize, power) then
  749. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,NR_EDI))
  750. else
  751. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,NR_EDI));
  752. end;
  753. {$ifndef __NOWINPECOFF__}
  754. { windows guards only a few pages for stack growing, }
  755. { so we have to access every page first }
  756. if target_info.system=system_i386_win32 then
  757. begin
  758. current_asmdata.getjumplabel(again);
  759. current_asmdata.getjumplabel(ok);
  760. a_label(list,again);
  761. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,NR_EDI));
  762. a_jmp_cond(list,OC_B,ok);
  763. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  764. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  765. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,NR_EDI));
  766. a_jmp_always(list,again);
  767. a_label(list,ok);
  768. end;
  769. {$endif __NOWINPECOFF__}
  770. { If we were probing pages, EDI=(size mod pagesize) and ESP is decremented
  771. by (size div pagesize)*pagesize, otherwise EDI=size.
  772. Either way, subtracting EDI from ESP will set ESP to desired final value. }
  773. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,NR_EDI,NR_ESP));
  774. { align stack on 4 bytes }
  775. list.concat(Taicpu.op_const_reg(A_AND,S_L,aint($fffffff4),NR_ESP));
  776. { load destination, don't use a_load_reg_reg, that will add a move instruction
  777. that can confuse the reg allocator }
  778. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,NR_ESP,NR_EDI));
  779. { Allocate ESI and load it with source }
  780. getcpuregister(list,NR_ESI);
  781. a_loadaddr_ref_reg(list,ref,NR_ESI);
  782. { calculate size }
  783. len:=elesize;
  784. opsize:=S_B;
  785. if (len and 3)=0 then
  786. begin
  787. opsize:=S_L;
  788. len:=len shr 2;
  789. end
  790. else
  791. if (len and 1)=0 then
  792. begin
  793. opsize:=S_W;
  794. len:=len shr 1;
  795. end;
  796. if len>1 then
  797. begin
  798. if ispowerof2(len, power) then
  799. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,NR_ECX))
  800. else
  801. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,NR_ECX));
  802. end;
  803. list.concat(Taicpu.op_none(A_REP,S_NO));
  804. case opsize of
  805. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  806. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  807. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  808. end;
  809. ungetcpuregister(list,NR_EDI);
  810. ungetcpuregister(list,NR_ECX);
  811. ungetcpuregister(list,NR_ESI);
  812. { patch the new address, but don't use a_load_reg_reg, that will add a move instruction
  813. that can confuse the reg allocator }
  814. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,NR_ESP,destreg));
  815. end;
  816. procedure tcg8086.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  817. begin
  818. { Nothing to release }
  819. end;
  820. procedure tcg8086.g_exception_reason_save(list : TAsmList; const href : treference);
  821. begin
  822. if not paramanager.use_fixed_stack then
  823. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_INT],NR_FUNCTION_RESULT_REG))
  824. else
  825. inherited g_exception_reason_save(list,href);
  826. end;
  827. procedure tcg8086.g_exception_reason_save_const(list : TAsmList;const href : treference; a: tcgint);
  828. begin
  829. if not paramanager.use_fixed_stack then
  830. list.concat(Taicpu.op_const(A_PUSH,tcgsize2opsize[OS_INT],a))
  831. else
  832. inherited g_exception_reason_save_const(list,href,a);
  833. end;
  834. procedure tcg8086.g_exception_reason_load(list : TAsmList; const href : treference);
  835. begin
  836. if not paramanager.use_fixed_stack then
  837. begin
  838. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  839. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[OS_INT],NR_FUNCTION_RESULT_REG))
  840. end
  841. else
  842. inherited g_exception_reason_load(list,href);
  843. end;
  844. procedure tcg8086.g_maybe_got_init(list: TAsmList);
  845. var
  846. notdarwin: boolean;
  847. begin
  848. { allocate PIC register }
  849. if (cs_create_pic in current_settings.moduleswitches) and
  850. (tf_pic_uses_got in target_info.flags) and
  851. (pi_needs_got in current_procinfo.flags) then
  852. begin
  853. notdarwin:=not(target_info.system in [system_i386_darwin,system_i386_iphonesim]);
  854. { on darwin, the got register is virtual (and allocated earlier
  855. already) }
  856. if notdarwin then
  857. { ecx could be used in leaf procedures that don't use ecx to pass
  858. aparameter }
  859. current_procinfo.got:=NR_EBX;
  860. if notdarwin { needs testing before it can be enabled for non-darwin platforms
  861. and
  862. (current_settings.optimizecputype in [cpu_Pentium2,cpu_Pentium3,cpu_Pentium4]) } then
  863. begin
  864. current_module.requires_ebx_pic_helper:=true;
  865. cg.a_call_name_static(list,'fpc_geteipasebx');
  866. end
  867. else
  868. begin
  869. { call/pop is faster than call/ret/mov on Core Solo and later
  870. according to Apple's benchmarking -- and all Intel Macs
  871. have at least a Core Solo (furthermore, the i386 - Pentium 1
  872. don't have a return stack buffer) }
  873. a_call_name_static(list,current_procinfo.CurrGOTLabel.name);
  874. a_label(list,current_procinfo.CurrGotLabel);
  875. list.concat(taicpu.op_reg(A_POP,S_L,current_procinfo.got))
  876. end;
  877. if notdarwin then
  878. begin
  879. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),0,NR_PIC_OFFSET_REG));
  880. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  881. end;
  882. end;
  883. end;
  884. procedure tcg8086.get_32bit_ops(op: TOpCG; out op1, op2: TAsmOp);
  885. begin
  886. case op of
  887. OP_ADD :
  888. begin
  889. op1:=A_ADD;
  890. op2:=A_ADC;
  891. end;
  892. OP_SUB :
  893. begin
  894. op1:=A_SUB;
  895. op2:=A_SBB;
  896. end;
  897. OP_XOR :
  898. begin
  899. op1:=A_XOR;
  900. op2:=A_XOR;
  901. end;
  902. OP_OR :
  903. begin
  904. op1:=A_OR;
  905. op2:=A_OR;
  906. end;
  907. OP_AND :
  908. begin
  909. op1:=A_AND;
  910. op2:=A_AND;
  911. end;
  912. else
  913. internalerror(200203241);
  914. end;
  915. end;
  916. procedure tcg8086.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  917. {
  918. possible calling conventions:
  919. default stdcall cdecl pascal register
  920. default(0): OK OK OK OK OK
  921. virtual(1): OK OK OK OK OK(2)
  922. (0):
  923. set self parameter to correct value
  924. jmp mangledname
  925. (1): The wrapper code use %eax to reach the virtual method address
  926. set self to correct value
  927. move self,%eax
  928. mov 0(%eax),%eax ; load vmt
  929. jmp vmtoffs(%eax) ; method offs
  930. (2): Virtual use values pushed on stack to reach the method address
  931. so the following code be generated:
  932. set self to correct value
  933. push %ebx ; allocate space for function address
  934. push %eax
  935. mov self,%eax
  936. mov 0(%eax),%eax ; load vmt
  937. mov vmtoffs(%eax),eax ; method offs
  938. mov %eax,4(%esp)
  939. pop %eax
  940. ret 0; jmp the address
  941. }
  942. procedure getselftoeax(offs: longint);
  943. var
  944. href : treference;
  945. selfoffsetfromsp : longint;
  946. begin
  947. { mov offset(%esp),%eax }
  948. if (procdef.proccalloption<>pocall_register) then
  949. begin
  950. { framepointer is pushed for nested procs }
  951. if procdef.parast.symtablelevel>normal_function_level then
  952. selfoffsetfromsp:=2*sizeof(aint)
  953. else
  954. selfoffsetfromsp:=sizeof(aint);
  955. reference_reset_base(href,NR_ESP,selfoffsetfromsp+offs,4);
  956. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  957. end;
  958. end;
  959. procedure loadvmttoeax;
  960. var
  961. href : treference;
  962. begin
  963. { mov 0(%eax),%eax ; load vmt}
  964. reference_reset_base(href,NR_EAX,0,4);
  965. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  966. end;
  967. procedure op_oneaxmethodaddr(op: TAsmOp);
  968. var
  969. href : treference;
  970. begin
  971. if (procdef.extnumber=$ffff) then
  972. Internalerror(200006139);
  973. { call/jmp vmtoffs(%eax) ; method offs }
  974. reference_reset_base(href,NR_EAX,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  975. list.concat(taicpu.op_ref(op,S_L,href));
  976. end;
  977. procedure loadmethodoffstoeax;
  978. var
  979. href : treference;
  980. begin
  981. if (procdef.extnumber=$ffff) then
  982. Internalerror(200006139);
  983. { mov vmtoffs(%eax),%eax ; method offs }
  984. reference_reset_base(href,NR_EAX,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  985. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EAX);
  986. end;
  987. var
  988. lab : tasmsymbol;
  989. make_global : boolean;
  990. href : treference;
  991. begin
  992. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  993. Internalerror(200006137);
  994. if not assigned(procdef.struct) or
  995. (procdef.procoptions*[po_classmethod, po_staticmethod,
  996. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  997. Internalerror(200006138);
  998. if procdef.owner.symtabletype<>ObjectSymtable then
  999. Internalerror(200109191);
  1000. make_global:=false;
  1001. if (not current_module.is_unit) or
  1002. create_smartlink or
  1003. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1004. make_global:=true;
  1005. if make_global then
  1006. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1007. else
  1008. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1009. { set param1 interface to self }
  1010. g_adjust_self_value(list,procdef,ioffset);
  1011. if (po_virtualmethod in procdef.procoptions) and
  1012. not is_objectpascal_helper(procdef.struct) then
  1013. begin
  1014. if (procdef.proccalloption=pocall_register) then
  1015. begin
  1016. { case 2 }
  1017. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EBX)); { allocate space for address}
  1018. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1019. getselftoeax(8);
  1020. loadvmttoeax;
  1021. loadmethodoffstoeax;
  1022. { mov %eax,4(%esp) }
  1023. reference_reset_base(href,NR_ESP,4,4);
  1024. list.concat(taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1025. { pop %eax }
  1026. list.concat(taicpu.op_reg(A_POP,S_L,NR_EAX));
  1027. { ret ; jump to the address }
  1028. list.concat(taicpu.op_none(A_RET,S_L));
  1029. end
  1030. else
  1031. begin
  1032. { case 1 }
  1033. getselftoeax(0);
  1034. loadvmttoeax;
  1035. op_oneaxmethodaddr(A_JMP);
  1036. end;
  1037. end
  1038. { case 0 }
  1039. else
  1040. begin
  1041. if (target_info.system <> system_i386_darwin) then
  1042. begin
  1043. lab:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  1044. list.concat(taicpu.op_sym(A_JMP,S_NO,lab))
  1045. end
  1046. else
  1047. list.concat(taicpu.op_sym(A_JMP,S_NO,get_darwin_call_stub(procdef.mangledname,false)))
  1048. end;
  1049. List.concat(Tai_symbol_end.Createname(labelname));
  1050. end;
  1051. { ************* 64bit operations ************ }
  1052. procedure tcg64f386.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  1053. begin
  1054. case op of
  1055. OP_ADD :
  1056. begin
  1057. op1:=A_ADD;
  1058. op2:=A_ADC;
  1059. end;
  1060. OP_SUB :
  1061. begin
  1062. op1:=A_SUB;
  1063. op2:=A_SBB;
  1064. end;
  1065. OP_XOR :
  1066. begin
  1067. op1:=A_XOR;
  1068. op2:=A_XOR;
  1069. end;
  1070. OP_OR :
  1071. begin
  1072. op1:=A_OR;
  1073. op2:=A_OR;
  1074. end;
  1075. OP_AND :
  1076. begin
  1077. op1:=A_AND;
  1078. op2:=A_AND;
  1079. end;
  1080. else
  1081. internalerror(200203241);
  1082. end;
  1083. end;
  1084. procedure tcg64f386.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  1085. var
  1086. op1,op2 : TAsmOp;
  1087. tempref : treference;
  1088. begin
  1089. if not(op in [OP_NEG,OP_NOT]) then
  1090. begin
  1091. get_64bit_ops(op,op1,op2);
  1092. tempref:=ref;
  1093. tcgx86(cg).make_simple_ref(list,tempref);
  1094. list.concat(taicpu.op_ref_reg(op1,S_L,tempref,reg.reglo));
  1095. inc(tempref.offset,4);
  1096. list.concat(taicpu.op_ref_reg(op2,S_L,tempref,reg.reghi));
  1097. end
  1098. else
  1099. begin
  1100. a_load64_ref_reg(list,ref,reg);
  1101. a_op64_reg_reg(list,op,size,reg,reg);
  1102. end;
  1103. end;
  1104. procedure tcg64f386.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1105. var
  1106. op1,op2 : TAsmOp;
  1107. begin
  1108. case op of
  1109. OP_NEG :
  1110. begin
  1111. if (regsrc.reglo<>regdst.reglo) then
  1112. a_load64_reg_reg(list,regsrc,regdst);
  1113. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  1114. list.concat(taicpu.op_reg(A_NEG,S_L,regdst.reglo));
  1115. list.concat(taicpu.op_const_reg(A_SBB,S_L,-1,regdst.reghi));
  1116. exit;
  1117. end;
  1118. OP_NOT :
  1119. begin
  1120. if (regsrc.reglo<>regdst.reglo) then
  1121. a_load64_reg_reg(list,regsrc,regdst);
  1122. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  1123. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reglo));
  1124. exit;
  1125. end;
  1126. end;
  1127. get_64bit_ops(op,op1,op2);
  1128. list.concat(taicpu.op_reg_reg(op1,S_L,regsrc.reglo,regdst.reglo));
  1129. list.concat(taicpu.op_reg_reg(op2,S_L,regsrc.reghi,regdst.reghi));
  1130. end;
  1131. procedure tcg64f386.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1132. var
  1133. op1,op2 : TAsmOp;
  1134. begin
  1135. case op of
  1136. OP_AND,OP_OR,OP_XOR:
  1137. begin
  1138. cg.a_op_const_reg(list,op,OS_32,tcgint(lo(value)),reg.reglo);
  1139. cg.a_op_const_reg(list,op,OS_32,tcgint(hi(value)),reg.reghi);
  1140. end;
  1141. OP_ADD, OP_SUB:
  1142. begin
  1143. // can't use a_op_const_ref because this may use dec/inc
  1144. get_64bit_ops(op,op1,op2);
  1145. list.concat(taicpu.op_const_reg(op1,S_L,aint(lo(value)),reg.reglo));
  1146. list.concat(taicpu.op_const_reg(op2,S_L,aint(hi(value)),reg.reghi));
  1147. end;
  1148. else
  1149. internalerror(200204021);
  1150. end;
  1151. end;
  1152. procedure tcg64f386.a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);
  1153. var
  1154. op1,op2 : TAsmOp;
  1155. tempref : treference;
  1156. begin
  1157. tempref:=ref;
  1158. tcgx86(cg).make_simple_ref(list,tempref);
  1159. case op of
  1160. OP_AND,OP_OR,OP_XOR:
  1161. begin
  1162. cg.a_op_const_ref(list,op,OS_32,tcgint(lo(value)),tempref);
  1163. inc(tempref.offset,4);
  1164. cg.a_op_const_ref(list,op,OS_32,tcgint(hi(value)),tempref);
  1165. end;
  1166. OP_ADD, OP_SUB:
  1167. begin
  1168. get_64bit_ops(op,op1,op2);
  1169. // can't use a_op_const_ref because this may use dec/inc
  1170. list.concat(taicpu.op_const_ref(op1,S_L,aint(lo(value)),tempref));
  1171. inc(tempref.offset,4);
  1172. list.concat(taicpu.op_const_ref(op2,S_L,aint(hi(value)),tempref));
  1173. end;
  1174. else
  1175. internalerror(200204022);
  1176. end;
  1177. end;
  1178. procedure create_codegen;
  1179. begin
  1180. cg := tcg8086.create;
  1181. cg64 := tcg64f386.create;
  1182. end;
  1183. end.