cgcpu.pas 66 KB

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