cgcpu.pas 53 KB

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