cgcpu.pas 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the i8086
  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_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  34. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  35. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  36. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  37. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  38. procedure push_const(list:TAsmList;size:tcgsize;a:tcgint);
  39. { passing parameter using push instead of mov }
  40. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  41. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  42. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  43. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  44. { move instructions }
  45. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  46. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  47. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  48. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  49. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  50. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);override;
  51. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);override;
  52. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  53. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  54. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  55. procedure g_exception_reason_save(list : TAsmList; const href : treference);override;
  56. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);override;
  57. procedure g_exception_reason_load(list : TAsmList; const href : treference);override;
  58. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);override;
  59. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  60. procedure get_32bit_ops(op: TOpCG; out op1,op2: TAsmOp);
  61. end;
  62. tcg64f8086 = class(tcg64f32)
  63. { procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;}
  64. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  65. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  66. { procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);override;}
  67. private
  68. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  69. end;
  70. procedure create_codegen;
  71. implementation
  72. uses
  73. globals,verbose,systems,cutils,
  74. paramgr,procinfo,fmodule,
  75. rgcpu,rgx86,cpuinfo,
  76. symtype,symsym;
  77. function use_push(const cgpara:tcgpara):boolean;
  78. begin
  79. result:=(not paramanager.use_fixed_stack) and
  80. assigned(cgpara.location) and
  81. (cgpara.location^.loc=LOC_REFERENCE) and
  82. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  83. end;
  84. procedure tcg8086.init_register_allocators;
  85. begin
  86. inherited init_register_allocators;
  87. if not(target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  88. (cs_create_pic in current_settings.moduleswitches) then
  89. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_SI,RS_DI],first_int_imreg,[RS_BP])
  90. else
  91. if (cs_useebp in current_settings.optimizerswitches) and assigned(current_procinfo) and (current_procinfo.framepointer<>NR_BP) then
  92. 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,[])
  93. else
  94. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_BX,RS_SI,RS_DI],first_int_imreg,[RS_BP]);
  95. 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,[]);
  96. 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,[]);
  97. rgfpu:=Trgx86fpu.create;
  98. end;
  99. procedure tcg8086.do_register_allocation(list:TAsmList;headertai:tai);
  100. begin
  101. if (pi_needs_got in current_procinfo.flags) then
  102. begin
  103. if getsupreg(current_procinfo.got) < first_int_imreg then
  104. include(rg[R_INTREGISTER].used_in_proc,getsupreg(current_procinfo.got));
  105. end;
  106. inherited do_register_allocation(list,headertai);
  107. end;
  108. function tcg8086.getintregister(list: TAsmList; size: Tcgsize): Tregister;
  109. begin
  110. case size of
  111. OS_8, OS_S8,
  112. OS_16, OS_S16:
  113. Result := inherited getintregister(list, size);
  114. OS_32, OS_S32:
  115. begin
  116. Result:=inherited getintregister(list, OS_16);
  117. { ensure that the high register can be retrieved by
  118. GetNextReg
  119. }
  120. if inherited getintregister(list, OS_16)<>GetNextReg(Result) then
  121. internalerror(2013030202);
  122. end;
  123. else
  124. internalerror(2013030201);
  125. end;
  126. end;
  127. procedure tcg8086.a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize;
  128. a: tcgint; reg: TRegister);
  129. var
  130. tmpreg: tregister;
  131. op1, op2: TAsmOp;
  132. ax_subreg: tregister;
  133. hl_loop_start: tasmlabel;
  134. ai: taicpu;
  135. use_loop: Boolean;
  136. i: Integer;
  137. begin
  138. optimize_op_const(op, a);
  139. check_register_size(size,reg);
  140. if size in [OS_64, OS_S64] then
  141. internalerror(2013030904);
  142. if size in [OS_32, OS_S32] then
  143. begin
  144. case op of
  145. OP_NONE:
  146. begin
  147. { Opcode is optimized away }
  148. end;
  149. OP_MOVE:
  150. begin
  151. { Optimized, replaced with a simple load }
  152. a_load_const_reg(list,size,a,reg);
  153. end;
  154. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  155. begin
  156. if (longword(a) = high(longword)) and
  157. (op in [OP_AND,OP_OR,OP_XOR]) then
  158. begin
  159. case op of
  160. OP_AND:
  161. exit;
  162. OP_OR:
  163. a_load_const_reg(list,size,high(longword),reg);
  164. OP_XOR:
  165. begin
  166. list.concat(taicpu.op_reg(A_NOT,S_W,reg));
  167. list.concat(taicpu.op_reg(A_NOT,S_W,GetNextReg(reg)));
  168. end;
  169. end
  170. end
  171. else
  172. begin
  173. get_32bit_ops(op, op1, op2);
  174. list.concat(taicpu.op_const_reg(op1,S_W,aint(a and $FFFF),reg));
  175. list.concat(taicpu.op_const_reg(op2,S_W,aint(a shr 16),GetNextReg(reg)));
  176. end;
  177. end;
  178. OP_SHR,OP_SHL,OP_SAR:
  179. begin
  180. a:=a and 31;
  181. { for shl with const >= 16, we can just move the low register
  182. to the high reg, then zero the low register, then do the
  183. remaining part of the shift (by const-16) in 16 bit on the
  184. high register. the same thing applies to shr with low and high
  185. reversed. sar is exactly like shr, except that instead of
  186. zeroing the high register, we sar it by 15. }
  187. if a>=16 then
  188. case op of
  189. OP_SHR:
  190. begin
  191. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),reg);
  192. a_load_const_reg(list,OS_16,0,GetNextReg(reg));
  193. a_op_const_reg(list,OP_SHR,OS_16,a-16,reg);
  194. end;
  195. OP_SHL:
  196. begin
  197. a_load_reg_reg(list,OS_16,OS_16,reg,GetNextReg(reg));
  198. a_load_const_reg(list,OS_16,0,reg);
  199. a_op_const_reg(list,OP_SHL,OS_16,a-16,GetNextReg(reg));
  200. end;
  201. OP_SAR:
  202. begin
  203. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),reg);
  204. a_op_const_reg(list,OP_SAR,OS_16,15,GetNextReg(reg));
  205. a_op_const_reg(list,OP_SAR,OS_16,a-16,reg);
  206. end;
  207. else
  208. internalerror(2013060201);
  209. end
  210. else if a<>0 then
  211. begin
  212. use_loop:=a>2;
  213. if use_loop then
  214. begin
  215. getcpuregister(list,NR_CX);
  216. a_load_const_reg(list,OS_16,a,NR_CX);
  217. current_asmdata.getjumplabel(hl_loop_start);
  218. a_label(list,hl_loop_start);
  219. case op of
  220. OP_SHR:
  221. begin
  222. list.concat(taicpu.op_const_reg(A_SHR,S_W,1,GetNextReg(reg)));
  223. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  224. end;
  225. OP_SAR:
  226. begin
  227. list.concat(taicpu.op_const_reg(A_SAR,S_W,1,GetNextReg(reg)));
  228. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  229. end;
  230. OP_SHL:
  231. begin
  232. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,reg));
  233. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg)));
  234. end;
  235. else
  236. internalerror(2013030903);
  237. end;
  238. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  239. ai.is_jmp:=true;
  240. list.concat(ai);
  241. ungetcpuregister(list,NR_CX);
  242. end
  243. else
  244. begin
  245. for i:=1 to a do
  246. begin
  247. case op of
  248. OP_SHR:
  249. begin
  250. list.concat(taicpu.op_const_reg(A_SHR,S_W,1,GetNextReg(reg)));
  251. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  252. end;
  253. OP_SAR:
  254. begin
  255. list.concat(taicpu.op_const_reg(A_SAR,S_W,1,GetNextReg(reg)));
  256. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  257. end;
  258. OP_SHL:
  259. begin
  260. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,reg));
  261. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg)));
  262. end;
  263. else
  264. internalerror(2013030903);
  265. end;
  266. end;
  267. end;
  268. end;
  269. end;
  270. else
  271. begin
  272. tmpreg:=getintregister(list,size);
  273. a_load_const_reg(list,size,a,tmpreg);
  274. a_op_reg_reg(list,op,size,tmpreg,reg);
  275. end;
  276. end;
  277. end
  278. else
  279. begin
  280. { size <= 16-bit }
  281. { 8086 doesn't support 'imul reg,const', so we handle it here }
  282. if (current_settings.cputype<cpu_186) and (op in [OP_MUL,OP_IMUL]) then
  283. begin
  284. { TODO: also enable the SHL optimization below }
  285. { if not(cs_check_overflow in current_settings.localswitches) and
  286. ispowerof2(int64(a),power) then
  287. begin
  288. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  289. exit;
  290. end;}
  291. if op = OP_IMUL then
  292. begin
  293. if size in [OS_16,OS_S16] then
  294. ax_subreg := NR_AX
  295. else
  296. if size in [OS_8,OS_S8] then
  297. ax_subreg := NR_AL
  298. else
  299. internalerror(2013050102);
  300. getcpuregister(list,NR_AX);
  301. if size in [OS_16,OS_S16] then
  302. getcpuregister(list,NR_DX);
  303. a_load_const_reg(list,size,a,ax_subreg);
  304. list.concat(taicpu.op_reg(A_IMUL,TCgSize2OpSize[size],reg));
  305. a_load_reg_reg(list,size,size,ax_subreg,reg);
  306. ungetcpuregister(list,NR_AX);
  307. if size in [OS_16,OS_S16] then
  308. ungetcpuregister(list,NR_DX);
  309. { TODO: implement overflow checking? }
  310. exit;
  311. end
  312. else
  313. { OP_MUL should be handled specifically in the code }
  314. { generator because of the silly register usage restraints }
  315. internalerror(200109225);
  316. end
  317. else
  318. inherited a_op_const_reg(list, Op, size, a, reg);
  319. end;
  320. end;
  321. procedure tcg8086.a_op_const_ref(list: TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  322. var
  323. tmpref: treference;
  324. op1,op2: TAsmOp;
  325. begin
  326. optimize_op_const(op, a);
  327. tmpref:=ref;
  328. make_simple_ref(list,tmpref);
  329. if size in [OS_64, OS_S64] then
  330. internalerror(2013050801);
  331. if size in [OS_32, OS_S32] then
  332. begin
  333. case Op of
  334. OP_NONE :
  335. begin
  336. { Opcode is optimized away }
  337. end;
  338. OP_MOVE :
  339. begin
  340. { Optimized, replaced with a simple load }
  341. a_load_const_ref(list,size,a,ref);
  342. end;
  343. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  344. begin
  345. if (longword(a) = high(longword)) and
  346. (op in [OP_AND,OP_OR,OP_XOR]) then
  347. begin
  348. case op of
  349. OP_AND:
  350. exit;
  351. OP_OR:
  352. a_load_const_ref(list,size,high(longword),tmpref);
  353. OP_XOR:
  354. begin
  355. list.concat(taicpu.op_ref(A_NOT,S_W,tmpref));
  356. inc(tmpref.offset, 2);
  357. list.concat(taicpu.op_ref(A_NOT,S_W,tmpref));
  358. end;
  359. end
  360. end
  361. else
  362. begin
  363. get_32bit_ops(op, op1, op2);
  364. list.concat(taicpu.op_const_ref(op1,S_W,aint(a and $FFFF),tmpref));
  365. inc(tmpref.offset, 2);
  366. list.concat(taicpu.op_const_ref(op2,S_W,aint(a shr 16),tmpref));
  367. end;
  368. end;
  369. else
  370. internalerror(2013050802);
  371. end;
  372. end
  373. else
  374. inherited a_op_const_ref(list,Op,size,a,tmpref);
  375. end;
  376. procedure tcg8086.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize;
  377. src, dst: TRegister);
  378. var
  379. op1, op2: TAsmOp;
  380. hl_skip, hl_loop_start: TAsmLabel;
  381. ai: taicpu;
  382. begin
  383. check_register_size(size,src);
  384. check_register_size(size,dst);
  385. if size in [OS_64, OS_S64] then
  386. internalerror(2013030902);
  387. if size in [OS_32, OS_S32] then
  388. begin
  389. case op of
  390. OP_NEG:
  391. begin
  392. if src<>dst then
  393. a_load_reg_reg(list,size,size,src,dst);
  394. list.concat(taicpu.op_reg(A_NOT, S_W, GetNextReg(dst)));
  395. list.concat(taicpu.op_reg(A_NEG, S_W, dst));
  396. list.concat(taicpu.op_const_reg(A_SBB, S_W,-1, GetNextReg(dst)));
  397. end;
  398. OP_NOT:
  399. begin
  400. if src<>dst then
  401. a_load_reg_reg(list,size,size,src,dst);
  402. list.concat(taicpu.op_reg(A_NOT, S_W, dst));
  403. list.concat(taicpu.op_reg(A_NOT, S_W, GetNextReg(dst)));
  404. end;
  405. OP_ADD,OP_SUB,OP_XOR,OP_OR,OP_AND:
  406. begin
  407. get_32bit_ops(op, op1, op2);
  408. list.concat(taicpu.op_reg_reg(op1, S_W, src, dst));
  409. list.concat(taicpu.op_reg_reg(op2, S_W, GetNextReg(src), GetNextReg(dst)));
  410. end;
  411. OP_SHR,OP_SHL,OP_SAR:
  412. begin
  413. getcpuregister(list,NR_CX);
  414. a_load_reg_reg(list,size,OS_16,src,NR_CX);
  415. list.concat(taicpu.op_const_reg(A_AND,S_W,$1f,NR_CX));
  416. current_asmdata.getjumplabel(hl_skip);
  417. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl_skip);
  418. ai.SetCondition(C_Z);
  419. ai.is_jmp:=true;
  420. list.concat(ai);
  421. current_asmdata.getjumplabel(hl_loop_start);
  422. a_label(list,hl_loop_start);
  423. case op of
  424. OP_SHR:
  425. begin
  426. list.concat(taicpu.op_const_reg(A_SHR,S_W,1,GetNextReg(dst)));
  427. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,dst));
  428. end;
  429. OP_SAR:
  430. begin
  431. list.concat(taicpu.op_const_reg(A_SAR,S_W,1,GetNextReg(dst)));
  432. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,dst));
  433. end;
  434. OP_SHL:
  435. begin
  436. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,dst));
  437. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(dst)));
  438. end;
  439. else
  440. internalerror(2013030903);
  441. end;
  442. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  443. ai.is_jmp:=true;
  444. list.concat(ai);
  445. a_label(list,hl_skip);
  446. ungetcpuregister(list,NR_CX);
  447. end;
  448. else
  449. internalerror(2013030901);
  450. end;
  451. end
  452. else
  453. inherited a_op_reg_reg(list, Op, size, src, dst);
  454. end;
  455. procedure tcg8086.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  456. var
  457. tmpref : treference;
  458. op1, op2: TAsmOp;
  459. begin
  460. tmpref:=ref;
  461. make_simple_ref(list,tmpref);
  462. check_register_size(size,reg);
  463. if size in [OS_64, OS_S64] then
  464. internalerror(2013030902);
  465. if size in [OS_32, OS_S32] then
  466. begin
  467. case op of
  468. OP_ADD,OP_SUB,OP_XOR,OP_OR,OP_AND:
  469. begin
  470. get_32bit_ops(op, op1, op2);
  471. list.concat(taicpu.op_ref_reg(op1, S_W, tmpref, reg));
  472. inc(tmpref.offset, 2);
  473. list.concat(taicpu.op_ref_reg(op2, S_W, tmpref, GetNextReg(reg)));
  474. end;
  475. else
  476. internalerror(2013050701);
  477. end;
  478. end
  479. else
  480. inherited a_op_ref_reg(list,Op,size,tmpref,reg);
  481. end;
  482. procedure tcg8086.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  483. var
  484. tmpref: treference;
  485. op1,op2: TAsmOp;
  486. begin
  487. tmpref:=ref;
  488. make_simple_ref(list,tmpref);
  489. check_register_size(size,reg);
  490. if size in [OS_64, OS_S64] then
  491. internalerror(2013050803);
  492. if size in [OS_32, OS_S32] then
  493. begin
  494. case op of
  495. OP_NEG:
  496. begin
  497. if reg<>NR_NO then
  498. internalerror(200109237);
  499. inc(tmpref.offset, 2);
  500. list.concat(taicpu.op_ref(A_NOT, S_W, tmpref));
  501. dec(tmpref.offset, 2);
  502. list.concat(taicpu.op_ref(A_NEG, S_W, tmpref));
  503. inc(tmpref.offset, 2);
  504. list.concat(taicpu.op_const_ref(A_SBB, S_W,-1, tmpref));
  505. end;
  506. OP_NOT:
  507. begin
  508. if reg<>NR_NO then
  509. internalerror(200109237);
  510. list.concat(taicpu.op_ref(A_NOT, S_W, tmpref));
  511. inc(tmpref.offset, 2);
  512. list.concat(taicpu.op_ref(A_NOT, S_W, tmpref));
  513. end;
  514. OP_IMUL:
  515. begin
  516. { this one needs a load/imul/store, which is the default }
  517. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  518. end;
  519. OP_MUL,OP_DIV,OP_IDIV:
  520. { special stuff, needs separate handling inside code }
  521. { generator }
  522. internalerror(200109238);
  523. OP_ADD,OP_SUB,OP_XOR,OP_OR,OP_AND:
  524. begin
  525. get_32bit_ops(op, op1, op2);
  526. list.concat(taicpu.op_reg_ref(op1, S_W, reg, tmpref));
  527. inc(tmpref.offset, 2);
  528. list.concat(taicpu.op_reg_ref(op2, S_W, GetNextReg(reg), tmpref));
  529. end;
  530. else
  531. internalerror(2013050804);
  532. end;
  533. end
  534. else
  535. inherited a_op_reg_ref(list,Op,size,reg,tmpref);
  536. end;
  537. procedure tcg8086.push_const(list: TAsmList; size: tcgsize; a: tcgint);
  538. var
  539. tmpreg: TRegister;
  540. begin
  541. if not (size in [OS_16,OS_S16]) then
  542. internalerror(2013043001);
  543. if current_settings.cputype < cpu_186 then
  544. begin
  545. tmpreg:=getintregister(list,size);
  546. a_load_const_reg(list,size,a,tmpreg);
  547. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  548. end
  549. else
  550. list.concat(taicpu.op_const(A_PUSH,TCGSize2OpSize[size],a));
  551. end;
  552. procedure tcg8086.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  553. var
  554. pushsize, pushsize2: tcgsize;
  555. begin
  556. check_register_size(size,r);
  557. if use_push(cgpara) then
  558. begin
  559. if tcgsize2size[cgpara.Size] > 2 then
  560. begin
  561. if tcgsize2size[cgpara.Size] <> 4 then
  562. internalerror(2013031101);
  563. if cgpara.location^.Next = nil then
  564. begin
  565. if tcgsize2size[cgpara.location^.size] <> 4 then
  566. internalerror(2013031101);
  567. end
  568. else
  569. begin
  570. if tcgsize2size[cgpara.location^.size] <> 2 then
  571. internalerror(2013031101);
  572. if tcgsize2size[cgpara.location^.Next^.size] <> 2 then
  573. internalerror(2013031101);
  574. if cgpara.location^.Next^.Next <> nil then
  575. internalerror(2013031101);
  576. end;
  577. if tcgsize2size[cgpara.size]>cgpara.alignment then
  578. pushsize:=cgpara.size
  579. else
  580. pushsize:=int_cgsize(cgpara.alignment);
  581. pushsize2 := int_cgsize(tcgsize2size[pushsize] - 2);
  582. list.concat(taicpu.op_reg(A_PUSH,TCgsize2opsize[pushsize2],makeregsize(list,GetNextReg(r),pushsize2)));
  583. list.concat(taicpu.op_reg(A_PUSH,S_W,makeregsize(list,r,OS_16)));
  584. end
  585. else
  586. begin
  587. cgpara.check_simple_location;
  588. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  589. pushsize:=cgpara.location^.size
  590. else
  591. pushsize:=int_cgsize(cgpara.alignment);
  592. list.concat(taicpu.op_reg(A_PUSH,TCgsize2opsize[pushsize],makeregsize(list,r,pushsize)));
  593. end;
  594. end
  595. else
  596. inherited a_load_reg_cgpara(list,size,r,cgpara);
  597. end;
  598. procedure tcg8086.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  599. var
  600. pushsize : tcgsize;
  601. begin
  602. if use_push(cgpara) then
  603. begin
  604. if tcgsize2size[cgpara.Size] > 2 then
  605. begin
  606. if tcgsize2size[cgpara.Size] <> 4 then
  607. internalerror(2013031101);
  608. if cgpara.location^.Next = nil then
  609. begin
  610. if tcgsize2size[cgpara.location^.size] <> 4 then
  611. internalerror(2013031101);
  612. end
  613. else
  614. begin
  615. if tcgsize2size[cgpara.location^.size] <> 2 then
  616. internalerror(2013031101);
  617. if tcgsize2size[cgpara.location^.Next^.size] <> 2 then
  618. internalerror(2013031101);
  619. if cgpara.location^.Next^.Next <> nil then
  620. internalerror(2013031101);
  621. end;
  622. if (cgpara.alignment <> 4) and (cgpara.alignment <> 2) then
  623. internalerror(2013031101);
  624. push_const(list,OS_16,a shr 16);
  625. push_const(list,OS_16,a and $FFFF);
  626. end
  627. else
  628. begin
  629. cgpara.check_simple_location;
  630. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  631. pushsize:=cgpara.location^.size
  632. else
  633. pushsize:=int_cgsize(cgpara.alignment);
  634. push_const(list,pushsize,a);
  635. end;
  636. end
  637. else
  638. inherited a_load_const_cgpara(list,size,a,cgpara);
  639. end;
  640. procedure tcg8086.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  641. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  642. var
  643. pushsize : tcgsize;
  644. opsize : topsize;
  645. tmpreg : tregister;
  646. href,tmpref: treference;
  647. begin
  648. if not assigned(paraloc) then
  649. exit;
  650. if (paraloc^.loc<>LOC_REFERENCE) or
  651. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  652. (tcgsize2size[paraloc^.size]>4) then
  653. internalerror(200501162);
  654. { Pushes are needed in reverse order, add the size of the
  655. current location to the offset where to load from. This
  656. prevents wrong calculations for the last location when
  657. the size is not a power of 2 }
  658. if assigned(paraloc^.next) then
  659. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  660. { Push the data starting at ofs }
  661. href:=r;
  662. inc(href.offset,ofs);
  663. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  664. pushsize:=paraloc^.size
  665. else
  666. pushsize:=int_cgsize(cgpara.alignment);
  667. opsize:=TCgsize2opsize[pushsize];
  668. { for go32v2 we obtain OS_F32,
  669. but pushs is not valid, we need pushl }
  670. if opsize=S_FS then
  671. opsize:=S_W;
  672. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  673. begin
  674. tmpreg:=getintregister(list,pushsize);
  675. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  676. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  677. end
  678. else
  679. begin
  680. make_simple_ref(list,href);
  681. if tcgsize2size[pushsize] > 2 then
  682. begin
  683. tmpref := href;
  684. Inc(tmpref.offset, 2);
  685. list.concat(taicpu.op_ref(A_PUSH,TCgsize2opsize[int_cgsize(tcgsize2size[pushsize]-2)],tmpref));
  686. end;
  687. list.concat(taicpu.op_ref(A_PUSH,opsize,href));
  688. end;
  689. end;
  690. var
  691. len : tcgint;
  692. href : treference;
  693. begin
  694. { cgpara.size=OS_NO requires a copy on the stack }
  695. if use_push(cgpara) then
  696. begin
  697. { Record copy? }
  698. if (cgpara.size in [OS_NO,OS_F64]) or (size=OS_NO) then
  699. begin
  700. cgpara.check_simple_location;
  701. len:=align(cgpara.intsize,cgpara.alignment);
  702. g_stackpointer_alloc(list,len);
  703. reference_reset_base(href,NR_STACK_POINTER_REG,0,4);
  704. g_concatcopy(list,r,href,len);
  705. end
  706. else
  707. begin
  708. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  709. internalerror(200501161);
  710. { We need to push the data in reverse order,
  711. therefor we use a recursive algorithm }
  712. pushdata(cgpara.location,0);
  713. end
  714. end
  715. else
  716. inherited a_load_ref_cgpara(list,size,r,cgpara);
  717. end;
  718. procedure tcg8086.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  719. var
  720. tmpreg : tregister;
  721. opsize : topsize;
  722. tmpref : treference;
  723. begin
  724. with r do
  725. begin
  726. if use_push(cgpara) then
  727. begin
  728. cgpara.check_simple_location;
  729. opsize:=tcgsize2opsize[OS_ADDR];
  730. if (segment=NR_NO) and (base=NR_NO) and (index=NR_NO) then
  731. begin
  732. if assigned(symbol) then
  733. begin
  734. if current_settings.cputype < cpu_186 then
  735. begin
  736. tmpreg:=getaddressregister(list);
  737. a_loadaddr_ref_reg(list,r,tmpreg);
  738. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  739. end
  740. else
  741. list.concat(Taicpu.Op_sym_ofs(A_PUSH,opsize,symbol,offset));
  742. end
  743. else
  744. push_const(list,OS_ADDR,offset);
  745. end
  746. else if (segment=NR_NO) and (base=NR_NO) and (index<>NR_NO) and
  747. (offset=0) and (scalefactor=0) and (symbol=nil) then
  748. list.concat(Taicpu.Op_reg(A_PUSH,opsize,index))
  749. else if (segment=NR_NO) and (base<>NR_NO) and (index=NR_NO) and
  750. (offset=0) and (symbol=nil) then
  751. list.concat(Taicpu.Op_reg(A_PUSH,opsize,base))
  752. else
  753. begin
  754. tmpreg:=getaddressregister(list);
  755. a_loadaddr_ref_reg(list,r,tmpreg);
  756. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  757. end;
  758. end
  759. else
  760. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  761. end;
  762. end;
  763. procedure tcg8086.a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);
  764. begin
  765. check_register_size(tosize,reg);
  766. if tosize in [OS_S32,OS_32] then
  767. begin
  768. list.concat(taicpu.op_const_reg(A_MOV,S_W,longint(a and $ffff),reg));
  769. list.concat(taicpu.op_const_reg(A_MOV,S_W,longint(a shr 16),GetNextReg(reg)));
  770. end
  771. else
  772. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg));
  773. end;
  774. procedure tcg8086.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  775. var
  776. tmpref : treference;
  777. begin
  778. tmpref:=ref;
  779. make_simple_ref(list,tmpref);
  780. if tosize in [OS_S32,OS_32] then
  781. begin
  782. a_load_const_ref(list,OS_16,longint(a and $ffff),tmpref);
  783. inc(tmpref.offset,2);
  784. a_load_const_ref(list,OS_16,longint(a shr 16),tmpref);
  785. end
  786. else
  787. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  788. end;
  789. procedure tcg8086.a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);
  790. var
  791. tmpsize : tcgsize;
  792. tmpreg : tregister;
  793. tmpref : treference;
  794. begin
  795. tmpref:=ref;
  796. make_simple_ref(list,tmpref);
  797. check_register_size(fromsize,reg);
  798. case tosize of
  799. OS_8,OS_S8:
  800. if fromsize in [OS_8,OS_S8] then
  801. list.concat(taicpu.op_reg_ref(A_MOV, S_B, reg, tmpref))
  802. else
  803. internalerror(2013030310);
  804. OS_16,OS_S16:
  805. case fromsize of
  806. OS_8:
  807. begin
  808. reg := makeregsize(list, reg, OS_16);
  809. setsubreg(reg, R_SUBH);
  810. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg));
  811. setsubreg(reg, R_SUBW);
  812. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  813. end;
  814. OS_S8: internalerror(2013052503); { TODO }
  815. OS_16,OS_S16:
  816. begin
  817. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  818. end;
  819. else
  820. internalerror(2013030312);
  821. end;
  822. OS_32,OS_S32:
  823. case fromsize of
  824. OS_8:
  825. begin
  826. reg := makeregsize(list, reg, OS_16);
  827. setsubreg(reg, R_SUBH);
  828. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg));
  829. setsubreg(reg, R_SUBW);
  830. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  831. inc(tmpref.offset, 2);
  832. list.concat(taicpu.op_const_ref(A_MOV, S_W, 0, tmpref));
  833. end;
  834. OS_S8:
  835. internalerror(2013052501); { TODO }
  836. OS_16:
  837. begin
  838. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  839. inc(tmpref.offset, 2);
  840. list.concat(taicpu.op_const_ref(A_MOV, S_W, 0, tmpref));
  841. end;
  842. OS_S16:
  843. internalerror(2013052502); { TODO }
  844. OS_32,OS_S32:
  845. begin
  846. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  847. inc(tmpref.offset, 2);
  848. list.concat(taicpu.op_reg_ref(A_MOV, S_W, GetNextReg(reg), tmpref));
  849. end;
  850. else
  851. internalerror(2013030313);
  852. end;
  853. else
  854. internalerror(2013030311);
  855. end;
  856. end;
  857. procedure tcg8086.a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);
  858. procedure add_mov(instr: Taicpu);
  859. begin
  860. { Notify the register allocator that we have written a move instruction so
  861. it can try to eliminate it. }
  862. if (instr.oper[0]^.reg<>current_procinfo.framepointer) and (instr.oper[0]^.reg<>NR_STACK_POINTER_REG) then
  863. add_move_instruction(instr);
  864. list.concat(instr);
  865. end;
  866. var
  867. tmpref : treference;
  868. begin
  869. tmpref:=ref;
  870. make_simple_ref(list,tmpref);
  871. check_register_size(tosize,reg);
  872. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  873. internalerror(2011021307);
  874. { if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  875. fromsize:=tosize;}
  876. case tosize of
  877. OS_8,OS_S8:
  878. if fromsize in [OS_8,OS_S8] then
  879. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg))
  880. else
  881. internalerror(2013030210);
  882. OS_16,OS_S16:
  883. case fromsize of
  884. OS_8:
  885. begin
  886. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, reg));
  887. reg := makeregsize(list, reg, OS_8);
  888. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg));
  889. end;
  890. OS_S8:
  891. begin
  892. getcpuregister(list, NR_AX);
  893. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, NR_AL));
  894. list.concat(taicpu.op_none(A_CBW));
  895. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  896. ungetcpuregister(list, NR_AX);
  897. end;
  898. OS_16,OS_S16:
  899. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  900. else
  901. internalerror(2013030212);
  902. end;
  903. OS_32,OS_S32:
  904. case fromsize of
  905. OS_8:
  906. begin
  907. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg)));
  908. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, reg));
  909. reg := makeregsize(list, reg, OS_8);
  910. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg));
  911. end;
  912. OS_S8:
  913. begin
  914. getcpuregister(list, NR_AX);
  915. getcpuregister(list, NR_DX);
  916. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, NR_AL));
  917. list.concat(taicpu.op_none(A_CBW));
  918. list.concat(taicpu.op_none(A_CWD));
  919. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  920. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg)));
  921. ungetcpuregister(list, NR_AX);
  922. ungetcpuregister(list, NR_DX);
  923. end;
  924. OS_16:
  925. begin
  926. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  927. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg)));
  928. end;
  929. OS_S16:
  930. begin
  931. getcpuregister(list, NR_AX);
  932. getcpuregister(list, NR_DX);
  933. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, NR_AX));
  934. list.concat(taicpu.op_none(A_CWD));
  935. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  936. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg)));
  937. ungetcpuregister(list, NR_AX);
  938. ungetcpuregister(list, NR_DX);
  939. end;
  940. OS_32,OS_S32:
  941. begin
  942. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  943. inc(tmpref.offset, 2);
  944. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, GetNextReg(reg)));
  945. end;
  946. else
  947. internalerror(2013030213);
  948. end;
  949. else
  950. internalerror(2013030211);
  951. end;
  952. end;
  953. procedure tcg8086.a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);
  954. procedure add_mov(instr: Taicpu);
  955. begin
  956. { Notify the register allocator that we have written a move instruction so
  957. it can try to eliminate it. }
  958. if (instr.oper[0]^.reg<>current_procinfo.framepointer) and (instr.oper[0]^.reg<>NR_STACK_POINTER_REG) then
  959. add_move_instruction(instr);
  960. list.concat(instr);
  961. end;
  962. begin
  963. check_register_size(fromsize,reg1);
  964. check_register_size(tosize,reg2);
  965. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  966. begin
  967. if tosize in [OS_32, OS_S32] then
  968. internalerror(2013031801);
  969. reg1:=makeregsize(list,reg1,tosize);
  970. fromsize:=tosize;
  971. end;
  972. if (reg1<>reg2) then
  973. begin
  974. case tosize of
  975. OS_8,OS_S8:
  976. if fromsize in [OS_8,OS_S8] then
  977. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2))
  978. else
  979. internalerror(2013030210);
  980. OS_16,OS_S16:
  981. case fromsize of
  982. OS_8:
  983. begin
  984. reg2 := makeregsize(list, reg2, OS_8);
  985. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2));
  986. setsubreg(reg2,R_SUBH);
  987. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg2));
  988. end;
  989. OS_S8:
  990. begin
  991. getcpuregister(list, NR_AX);
  992. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, NR_AL));
  993. list.concat(taicpu.op_none(A_CBW));
  994. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  995. ungetcpuregister(list, NR_AX);
  996. end;
  997. OS_16,OS_S16:
  998. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  999. else
  1000. internalerror(2013030212);
  1001. end;
  1002. OS_32,OS_S32:
  1003. case fromsize of
  1004. OS_8:
  1005. begin
  1006. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, GetNextReg(reg2)));
  1007. reg2 := makeregsize(list, reg2, OS_8);
  1008. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2));
  1009. setsubreg(reg2,R_SUBH);
  1010. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg2));
  1011. end;
  1012. OS_S8:
  1013. begin
  1014. getcpuregister(list, NR_AX);
  1015. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, NR_AL));
  1016. getcpuregister(list, NR_DX);
  1017. list.concat(taicpu.op_none(A_CBW));
  1018. list.concat(taicpu.op_none(A_CWD));
  1019. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  1020. ungetcpuregister(list, NR_AX);
  1021. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg2)));
  1022. ungetcpuregister(list, NR_DX);
  1023. end;
  1024. OS_16:
  1025. begin
  1026. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  1027. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg2)));
  1028. end;
  1029. OS_S16:
  1030. begin
  1031. getcpuregister(list, NR_AX);
  1032. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, NR_AX));
  1033. getcpuregister(list, NR_DX);
  1034. list.concat(taicpu.op_none(A_CWD));
  1035. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  1036. ungetcpuregister(list, NR_AX);
  1037. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg2)));
  1038. ungetcpuregister(list, NR_DX);
  1039. end;
  1040. OS_32,OS_S32:
  1041. begin
  1042. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  1043. add_mov(taicpu.op_reg_reg(A_MOV, S_W, GetNextReg(reg1), GetNextReg(reg2)));
  1044. end;
  1045. else
  1046. internalerror(2013030213);
  1047. end;
  1048. else
  1049. internalerror(2013030211);
  1050. end;
  1051. end;
  1052. end;
  1053. procedure tcg8086.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1054. var
  1055. ai : taicpu;
  1056. hreg, hreg16 : tregister;
  1057. hl_skip: TAsmLabel;
  1058. invf: TResFlags;
  1059. begin
  1060. hreg:=makeregsize(list,reg,OS_8);
  1061. invf := f;
  1062. inverse_flags(invf);
  1063. list.concat(Taicpu.op_const_reg(A_MOV, S_B, 0, hreg));
  1064. current_asmdata.getjumplabel(hl_skip);
  1065. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl_skip);
  1066. ai.SetCondition(flags_to_cond(invf));
  1067. ai.is_jmp:=true;
  1068. list.concat(ai);
  1069. { 16-bit INC is shorter than 8-bit }
  1070. hreg16:=makeregsize(list,hreg,OS_16);
  1071. list.concat(Taicpu.op_reg(A_INC, S_W, hreg16));
  1072. a_label(list,hl_skip);
  1073. if reg<>hreg then
  1074. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1075. end;
  1076. procedure tcg8086.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1077. var
  1078. tmpreg : tregister;
  1079. begin
  1080. tmpreg:=getintregister(list,size);
  1081. g_flags2reg(list,size,f,tmpreg);
  1082. a_load_reg_ref(list,size,size,tmpreg,ref);
  1083. end;
  1084. procedure tcg8086.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  1085. var
  1086. stacksize : longint;
  1087. begin
  1088. { MMX needs to call EMMS }
  1089. if assigned(rg[R_MMXREGISTER]) and
  1090. (rg[R_MMXREGISTER].uses_registers) then
  1091. list.concat(Taicpu.op_none(A_EMMS,S_NO));
  1092. { remove stackframe }
  1093. if not nostackframe then
  1094. begin
  1095. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1096. begin
  1097. stacksize:=current_procinfo.calc_stackframe_size;
  1098. if (target_info.stackalign>4) and
  1099. ((stacksize <> 0) or
  1100. (pi_do_call in current_procinfo.flags) or
  1101. { can't detect if a call in this case -> use nostackframe }
  1102. { if you (think you) know what you are doing }
  1103. (po_assembler in current_procinfo.procdef.procoptions)) then
  1104. stacksize := align(stacksize+sizeof(aint),target_info.stackalign) - sizeof(aint);
  1105. if (stacksize<>0) then
  1106. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,stacksize,current_procinfo.framepointer);
  1107. end
  1108. else
  1109. begin
  1110. if current_settings.cputype < cpu_186 then
  1111. begin
  1112. list.concat(Taicpu.op_reg_reg(A_MOV, S_W, NR_BP, NR_SP));
  1113. list.concat(Taicpu.op_reg(A_POP, S_W, NR_BP));
  1114. end
  1115. else
  1116. list.concat(Taicpu.op_none(A_LEAVE,S_NO));
  1117. end;
  1118. list.concat(tai_regalloc.dealloc(current_procinfo.framepointer,nil));
  1119. end;
  1120. { return from interrupt }
  1121. if po_interrupt in current_procinfo.procdef.procoptions then
  1122. begin
  1123. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_ES));
  1124. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  1125. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DI));
  1126. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_SI));
  1127. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DX));
  1128. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_CX));
  1129. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_BX));
  1130. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_AX));
  1131. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  1132. end
  1133. { Routines with the poclearstack flag set use only a ret }
  1134. else if (current_procinfo.procdef.proccalloption in clearstack_pocalls) and
  1135. (not paramanager.use_fixed_stack) then
  1136. begin
  1137. { complex return values are removed from stack in C code PM }
  1138. { but not on win32 }
  1139. { and not for safecall with hidden exceptions, because the result }
  1140. { wich contains the exception is passed in EAX }
  1141. if (target_info.system <> system_i386_win32) and
  1142. not ((current_procinfo.procdef.proccalloption = pocall_safecall) and
  1143. (tf_safecall_exceptions in target_info.flags)) and
  1144. paramanager.ret_in_param(current_procinfo.procdef.returndef,
  1145. current_procinfo.procdef) then
  1146. list.concat(Taicpu.Op_const(A_RET,S_W,sizeof(aint)))
  1147. else
  1148. list.concat(Taicpu.Op_none(A_RET,S_NO));
  1149. end
  1150. { ... also routines with parasize=0 }
  1151. else if (parasize=0) then
  1152. list.concat(Taicpu.Op_none(A_RET,S_NO))
  1153. else
  1154. begin
  1155. { parameters are limited to 65535 bytes because ret allows only imm16 }
  1156. if (parasize>65535) then
  1157. CGMessage(cg_e_parasize_too_big);
  1158. list.concat(Taicpu.Op_const(A_RET,S_W,parasize));
  1159. end;
  1160. end;
  1161. procedure tcg8086.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  1162. var
  1163. power,len : longint;
  1164. opsize : topsize;
  1165. {$ifndef __NOWINPECOFF__}
  1166. again,ok : tasmlabel;
  1167. {$endif}
  1168. begin
  1169. { get stack space }
  1170. getcpuregister(list,NR_DI);
  1171. a_load_loc_reg(list,OS_INT,lenloc,NR_DI);
  1172. list.concat(Taicpu.op_reg(A_INC,S_W,NR_DI));
  1173. { Now DI contains (high+1). Copy it to CX for later use. }
  1174. getcpuregister(list,NR_CX);
  1175. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_DI,NR_CX));
  1176. if (elesize<>1) then
  1177. begin
  1178. if ispowerof2(elesize, power) then
  1179. list.concat(Taicpu.op_const_reg(A_SHL,S_W,power,NR_DI))
  1180. else
  1181. list.concat(Taicpu.op_const_reg(A_IMUL,S_W,elesize,NR_DI));
  1182. end;
  1183. {$ifndef __NOWINPECOFF__}
  1184. { windows guards only a few pages for stack growing, }
  1185. { so we have to access every page first }
  1186. if target_info.system=system_i386_win32 then
  1187. begin
  1188. current_asmdata.getjumplabel(again);
  1189. current_asmdata.getjumplabel(ok);
  1190. a_label(list,again);
  1191. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,NR_EDI));
  1192. a_jmp_cond(list,OC_B,ok);
  1193. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1194. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1195. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,NR_EDI));
  1196. a_jmp_always(list,again);
  1197. a_label(list,ok);
  1198. end;
  1199. {$endif __NOWINPECOFF__}
  1200. { If we were probing pages, EDI=(size mod pagesize) and ESP is decremented
  1201. by (size div pagesize)*pagesize, otherwise EDI=size.
  1202. Either way, subtracting EDI from ESP will set ESP to desired final value. }
  1203. list.concat(Taicpu.op_reg_reg(A_SUB,S_W,NR_DI,NR_SP));
  1204. { align stack on 2 bytes }
  1205. list.concat(Taicpu.op_const_reg(A_AND,S_W,aint($fffe),NR_SP));
  1206. { load destination, don't use a_load_reg_reg, that will add a move instruction
  1207. that can confuse the reg allocator }
  1208. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SP,NR_DI));
  1209. {$ifdef volatile_es}
  1210. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_DS));
  1211. list.concat(taicpu.op_reg(A_POP,S_W,NR_ES));
  1212. {$endif volatile_es}
  1213. { Allocate SI and load it with source }
  1214. getcpuregister(list,NR_SI);
  1215. a_loadaddr_ref_reg(list,ref,NR_SI);
  1216. { calculate size }
  1217. len:=elesize;
  1218. opsize:=S_B;
  1219. { if (len and 3)=0 then
  1220. begin
  1221. opsize:=S_L;
  1222. len:=len shr 2;
  1223. end
  1224. else}
  1225. if (len and 1)=0 then
  1226. begin
  1227. opsize:=S_W;
  1228. len:=len shr 1;
  1229. end;
  1230. if len>1 then
  1231. begin
  1232. if ispowerof2(len, power) then
  1233. list.concat(Taicpu.op_const_reg(A_SHL,S_W,power,NR_CX))
  1234. else
  1235. list.concat(Taicpu.op_const_reg(A_IMUL,S_W,len,NR_CX));
  1236. end;
  1237. list.concat(Taicpu.op_none(A_REP,S_NO));
  1238. case opsize of
  1239. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1240. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1241. // S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1242. end;
  1243. ungetcpuregister(list,NR_DI);
  1244. ungetcpuregister(list,NR_CX);
  1245. ungetcpuregister(list,NR_SI);
  1246. { patch the new address, but don't use a_load_reg_reg, that will add a move instruction
  1247. that can confuse the reg allocator }
  1248. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SP,destreg));
  1249. end;
  1250. procedure tcg8086.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  1251. begin
  1252. { Nothing to release }
  1253. end;
  1254. procedure tcg8086.g_exception_reason_save(list : TAsmList; const href : treference);
  1255. begin
  1256. if not paramanager.use_fixed_stack then
  1257. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_INT],NR_FUNCTION_RESULT_REG))
  1258. else
  1259. inherited g_exception_reason_save(list,href);
  1260. end;
  1261. procedure tcg8086.g_exception_reason_save_const(list : TAsmList;const href : treference; a: tcgint);
  1262. begin
  1263. if not paramanager.use_fixed_stack then
  1264. push_const(list,OS_INT,a)
  1265. else
  1266. inherited g_exception_reason_save_const(list,href,a);
  1267. end;
  1268. procedure tcg8086.g_exception_reason_load(list : TAsmList; const href : treference);
  1269. begin
  1270. if not paramanager.use_fixed_stack then
  1271. begin
  1272. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  1273. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[OS_INT],NR_FUNCTION_RESULT_REG))
  1274. end
  1275. else
  1276. inherited g_exception_reason_load(list,href);
  1277. end;
  1278. procedure tcg8086.get_32bit_ops(op: TOpCG; out op1, op2: TAsmOp);
  1279. begin
  1280. case op of
  1281. OP_ADD :
  1282. begin
  1283. op1:=A_ADD;
  1284. op2:=A_ADC;
  1285. end;
  1286. OP_SUB :
  1287. begin
  1288. op1:=A_SUB;
  1289. op2:=A_SBB;
  1290. end;
  1291. OP_XOR :
  1292. begin
  1293. op1:=A_XOR;
  1294. op2:=A_XOR;
  1295. end;
  1296. OP_OR :
  1297. begin
  1298. op1:=A_OR;
  1299. op2:=A_OR;
  1300. end;
  1301. OP_AND :
  1302. begin
  1303. op1:=A_AND;
  1304. op2:=A_AND;
  1305. end;
  1306. else
  1307. internalerror(200203241);
  1308. end;
  1309. end;
  1310. procedure tcg8086.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1311. var
  1312. hsym : tsym;
  1313. href : treference;
  1314. paraloc : Pcgparalocation;
  1315. begin
  1316. { calculate the parameter info for the procdef }
  1317. procdef.init_paraloc_info(callerside);
  1318. hsym:=tsym(procdef.parast.Find('self'));
  1319. if not(assigned(hsym) and
  1320. (hsym.typ=paravarsym)) then
  1321. internalerror(200305251);
  1322. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1323. while paraloc<>nil do
  1324. with paraloc^ do
  1325. begin
  1326. case loc of
  1327. LOC_REGISTER:
  1328. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1329. LOC_REFERENCE:
  1330. begin
  1331. { offset in the wrapper needs to be adjusted for the stored
  1332. return address }
  1333. if (reference.index<>NR_BP) and (reference.index<>NR_BX) and (reference.index<>NR_DI)
  1334. and (reference.index<>NR_SI) then
  1335. begin
  1336. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_DI));
  1337. list.concat(taicpu.op_reg_reg(A_MOV,S_W,reference.index,NR_DI));
  1338. if reference.index=NR_SP then
  1339. reference_reset_base(href,NR_DI,reference.offset+sizeof(pint)+2,sizeof(pint))
  1340. else
  1341. reference_reset_base(href,NR_DI,reference.offset+sizeof(pint),sizeof(pint));
  1342. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  1343. list.concat(taicpu.op_reg(A_POP,S_W,NR_DI));
  1344. end
  1345. else
  1346. begin
  1347. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  1348. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  1349. end;
  1350. end
  1351. else
  1352. internalerror(200309189);
  1353. end;
  1354. paraloc:=next;
  1355. end;
  1356. end;
  1357. procedure tcg8086.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1358. {
  1359. possible calling conventions:
  1360. default stdcall cdecl pascal register
  1361. default(0): OK OK OK OK OK
  1362. virtual(1): OK OK OK OK OK(2)
  1363. (0):
  1364. set self parameter to correct value
  1365. jmp mangledname
  1366. (1): The wrapper code use %eax to reach the virtual method address
  1367. set self to correct value
  1368. move self,%bx
  1369. mov 0(%bx),%bx ; load vmt
  1370. jmp vmtoffs(%bx) ; method offs
  1371. (2): Virtual use values pushed on stack to reach the method address
  1372. so the following code be generated:
  1373. set self to correct value
  1374. push %bx ; allocate space for function address
  1375. push %bx
  1376. push %di
  1377. mov self,%bx
  1378. mov 0(%bx),%bx ; load vmt
  1379. mov vmtoffs(%bx),bx ; method offs
  1380. mov %sp,%di
  1381. mov %bx,4(%di)
  1382. pop %di
  1383. pop %bx
  1384. ret 0; jmp the address
  1385. }
  1386. procedure getselftobx(offs: longint);
  1387. var
  1388. href : treference;
  1389. selfoffsetfromsp : longint;
  1390. begin
  1391. { "mov offset(%sp),%bx" }
  1392. if (procdef.proccalloption<>pocall_register) then
  1393. begin
  1394. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_DI));
  1395. { framepointer is pushed for nested procs }
  1396. if procdef.parast.symtablelevel>normal_function_level then
  1397. selfoffsetfromsp:=2*sizeof(aint)
  1398. else
  1399. selfoffsetfromsp:=sizeof(aint);
  1400. list.concat(taicpu.op_reg_reg(A_mov,S_W,NR_SP,NR_DI));
  1401. reference_reset_base(href,NR_DI,selfoffsetfromsp+offs+2,2);
  1402. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_BX);
  1403. list.concat(taicpu.op_reg(A_POP,S_W,NR_DI));
  1404. end
  1405. else
  1406. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_BX,NR_BX);
  1407. end;
  1408. procedure loadvmttobx;
  1409. var
  1410. href : treference;
  1411. begin
  1412. { mov 0(%bx),%bx ; load vmt}
  1413. reference_reset_base(href,NR_BX,0,2);
  1414. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_BX);
  1415. end;
  1416. procedure loadmethodoffstobx;
  1417. var
  1418. href : treference;
  1419. begin
  1420. if (procdef.extnumber=$ffff) then
  1421. Internalerror(200006139);
  1422. { mov vmtoffs(%bx),%bx ; method offs }
  1423. reference_reset_base(href,NR_BX,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),2);
  1424. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_BX);
  1425. end;
  1426. var
  1427. lab : tasmsymbol;
  1428. make_global : boolean;
  1429. href : treference;
  1430. begin
  1431. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1432. Internalerror(200006137);
  1433. if not assigned(procdef.struct) or
  1434. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1435. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1436. Internalerror(200006138);
  1437. if procdef.owner.symtabletype<>ObjectSymtable then
  1438. Internalerror(200109191);
  1439. make_global:=false;
  1440. if (not current_module.is_unit) or
  1441. create_smartlink or
  1442. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1443. make_global:=true;
  1444. if make_global then
  1445. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1446. else
  1447. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1448. { set param1 interface to self }
  1449. g_adjust_self_value(list,procdef,ioffset);
  1450. if (po_virtualmethod in procdef.procoptions) and
  1451. not is_objectpascal_helper(procdef.struct) then
  1452. begin
  1453. { case 1 & case 2 }
  1454. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_BX)); { allocate space for address}
  1455. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_BX));
  1456. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_DI));
  1457. getselftobx(8);
  1458. loadvmttobx;
  1459. loadmethodoffstobx;
  1460. { set target address
  1461. "mov %bx,4(%sp)" }
  1462. reference_reset_base(href,NR_DI,4,2);
  1463. list.concat(taicpu.op_reg_reg(A_MOV,S_W,NR_SP,NR_DI));
  1464. list.concat(taicpu.op_reg_ref(A_MOV,S_W,NR_BX,href));
  1465. { load ax? }
  1466. if procdef.proccalloption=pocall_register then
  1467. list.concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BX,NR_AX));
  1468. { restore register
  1469. pop %di,bx }
  1470. list.concat(taicpu.op_reg(A_POP,S_W,NR_DI));
  1471. list.concat(taicpu.op_reg(A_POP,S_W,NR_BX));
  1472. { ret ; jump to the address }
  1473. list.concat(taicpu.op_none(A_RET,S_W));
  1474. end
  1475. { case 0 }
  1476. else
  1477. begin
  1478. lab:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  1479. list.concat(taicpu.op_sym(A_JMP,S_NO,lab))
  1480. end;
  1481. List.concat(Tai_symbol_end.Createname(labelname));
  1482. end;
  1483. { ************* 64bit operations ************ }
  1484. procedure tcg64f8086.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  1485. begin
  1486. case op of
  1487. OP_ADD :
  1488. begin
  1489. op1:=A_ADD;
  1490. op2:=A_ADC;
  1491. end;
  1492. OP_SUB :
  1493. begin
  1494. op1:=A_SUB;
  1495. op2:=A_SBB;
  1496. end;
  1497. OP_XOR :
  1498. begin
  1499. op1:=A_XOR;
  1500. op2:=A_XOR;
  1501. end;
  1502. OP_OR :
  1503. begin
  1504. op1:=A_OR;
  1505. op2:=A_OR;
  1506. end;
  1507. OP_AND :
  1508. begin
  1509. op1:=A_AND;
  1510. op2:=A_AND;
  1511. end;
  1512. else
  1513. internalerror(200203241);
  1514. end;
  1515. end;
  1516. (* procedure tcg64f8086.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  1517. var
  1518. op1,op2 : TAsmOp;
  1519. tempref : treference;
  1520. begin
  1521. if not(op in [OP_NEG,OP_NOT]) then
  1522. begin
  1523. get_64bit_ops(op,op1,op2);
  1524. tempref:=ref;
  1525. tcgx86(cg).make_simple_ref(list,tempref);
  1526. list.concat(taicpu.op_ref_reg(op1,S_L,tempref,reg.reglo));
  1527. inc(tempref.offset,4);
  1528. list.concat(taicpu.op_ref_reg(op2,S_L,tempref,reg.reghi));
  1529. end
  1530. else
  1531. begin
  1532. a_load64_ref_reg(list,ref,reg);
  1533. a_op64_reg_reg(list,op,size,reg,reg);
  1534. end;
  1535. end;*)
  1536. procedure tcg64f8086.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1537. var
  1538. op1,op2 : TAsmOp;
  1539. begin
  1540. case op of
  1541. OP_NEG :
  1542. begin
  1543. if (regsrc.reglo<>regdst.reglo) then
  1544. a_load64_reg_reg(list,regsrc,regdst);
  1545. cg.a_op_reg_reg(list,OP_NOT,OS_32,regdst.reghi,regdst.reghi);
  1546. cg.a_op_reg_reg(list,OP_NEG,OS_32,regdst.reglo,regdst.reglo);
  1547. { there's no OP_SBB, so do it directly }
  1548. list.concat(taicpu.op_const_reg(A_SBB,S_W,-1,regdst.reghi));
  1549. list.concat(taicpu.op_const_reg(A_SBB,S_W,-1,GetNextReg(regdst.reghi)));
  1550. exit;
  1551. end;
  1552. OP_NOT :
  1553. begin
  1554. if (regsrc.reglo<>regdst.reglo) then
  1555. a_load64_reg_reg(list,regsrc,regdst);
  1556. cg.a_op_reg_reg(list,OP_NOT,OS_32,regdst.reglo,regdst.reglo);
  1557. cg.a_op_reg_reg(list,OP_NOT,OS_32,regdst.reghi,regdst.reghi);
  1558. exit;
  1559. end;
  1560. end;
  1561. get_64bit_ops(op,op1,op2);
  1562. list.concat(taicpu.op_reg_reg(op1,S_W,regsrc.reglo,regdst.reglo));
  1563. list.concat(taicpu.op_reg_reg(op2,S_W,GetNextReg(regsrc.reglo),GetNextReg(regdst.reglo)));
  1564. list.concat(taicpu.op_reg_reg(op2,S_W,regsrc.reghi,regdst.reghi));
  1565. list.concat(taicpu.op_reg_reg(op2,S_W,GetNextReg(regsrc.reghi),GetNextReg(regdst.reghi)));
  1566. end;
  1567. procedure tcg64f8086.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1568. var
  1569. op1,op2 : TAsmOp;
  1570. begin
  1571. case op of
  1572. OP_AND,OP_OR,OP_XOR:
  1573. begin
  1574. cg.a_op_const_reg(list,op,OS_32,tcgint(lo(value)),reg.reglo);
  1575. cg.a_op_const_reg(list,op,OS_32,tcgint(hi(value)),reg.reghi);
  1576. end;
  1577. OP_ADD, OP_SUB:
  1578. begin
  1579. // can't use a_op_const_ref because this may use dec/inc
  1580. get_64bit_ops(op,op1,op2);
  1581. list.concat(taicpu.op_const_reg(op1,S_W,aint(value and $ffff),reg.reglo));
  1582. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 16) and $ffff),GetNextReg(reg.reglo)));
  1583. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 32) and $ffff),reg.reghi));
  1584. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 48) and $ffff),GetNextReg(reg.reghi)));
  1585. end;
  1586. else
  1587. internalerror(200204021);
  1588. end;
  1589. end;
  1590. (* procedure tcg64f8086.a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);
  1591. var
  1592. op1,op2 : TAsmOp;
  1593. tempref : treference;
  1594. begin
  1595. tempref:=ref;
  1596. tcgx86(cg).make_simple_ref(list,tempref);
  1597. case op of
  1598. OP_AND,OP_OR,OP_XOR:
  1599. begin
  1600. cg.a_op_const_ref(list,op,OS_32,tcgint(lo(value)),tempref);
  1601. inc(tempref.offset,4);
  1602. cg.a_op_const_ref(list,op,OS_32,tcgint(hi(value)),tempref);
  1603. end;
  1604. OP_ADD, OP_SUB:
  1605. begin
  1606. get_64bit_ops(op,op1,op2);
  1607. // can't use a_op_const_ref because this may use dec/inc
  1608. list.concat(taicpu.op_const_ref(op1,S_L,aint(lo(value)),tempref));
  1609. inc(tempref.offset,4);
  1610. list.concat(taicpu.op_const_ref(op2,S_L,aint(hi(value)),tempref));
  1611. end;
  1612. else
  1613. internalerror(200204022);
  1614. end;
  1615. end;*)
  1616. procedure create_codegen;
  1617. begin
  1618. cg := tcg8086.create;
  1619. cg64 := tcg64f8086.create;
  1620. end;
  1621. end.