2
0

cgcpu.pas 63 KB

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