cgx86.pas 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the common parts of the code generator for the i386 and the x86-64.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  19. }
  20. unit cgx86;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cginfo,cgbase,cgobj,
  25. aasmbase,aasmtai,aasmcpu,
  26. cpubase,cpuinfo,cpupara,
  27. node,symconst;
  28. type
  29. tcgx86 = class(tcg)
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);override;
  36. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  37. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  38. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  39. procedure a_call_name(list : taasmoutput;const s : string);override;
  40. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  41. procedure a_call_reg(list : taasmoutput;reg : tregister);override;
  42. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  43. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); override;
  44. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  45. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  46. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  47. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  48. size: tcgsize; a: aword; src, dst: tregister); override;
  49. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  50. size: tcgsize; src1, src2, dst: tregister); override;
  51. { move instructions }
  52. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  53. procedure a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);override;
  54. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  55. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;reg : tregister);override;
  56. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  57. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  58. { fpu move instructions }
  59. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  60. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  61. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  62. { vector register move instructions }
  63. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  64. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); override;
  65. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); override;
  66. procedure a_parammm_reg(list: taasmoutput; reg: tregister); override;
  67. { comparison operations }
  68. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  69. l : tasmlabel);override;
  70. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  71. l : tasmlabel);override;
  72. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  73. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  74. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  75. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  76. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); override;
  77. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference); override;
  78. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  79. procedure g_exception_reason_save(list : taasmoutput; const href : treference);override;
  80. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);override;
  81. procedure g_exception_reason_load(list : taasmoutput; const href : treference);override;
  82. class function reg_cgsize(const reg: tregister): tcgsize; override;
  83. { entry/exit code helpers }
  84. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);override;
  85. procedure g_removevaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);override;
  86. procedure g_interrupt_stackframe_entry(list : taasmoutput);override;
  87. procedure g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);override;
  88. procedure g_profilecode(list : taasmoutput);override;
  89. procedure g_stackpointer_alloc(list : taasmoutput;localsize : longint);override;
  90. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  91. procedure g_restore_frame_pointer(list : taasmoutput);override;
  92. procedure g_return_from_proc(list : taasmoutput;parasize : aword);override;
  93. {$ifndef TEST_GENERIC}
  94. procedure g_call_constructor_helper(list : taasmoutput);override;
  95. procedure g_call_destructor_helper(list : taasmoutput);override;
  96. procedure g_call_fail_helper(list : taasmoutput);override;
  97. {$endif}
  98. procedure g_save_standard_registers(list : taasmoutput; usedinproc : tregisterset);override;
  99. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : tregisterset);override;
  100. procedure g_save_all_registers(list : taasmoutput);override;
  101. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);override;
  102. procedure g_overflowcheck(list: taasmoutput; const p: tnode);override;
  103. private
  104. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  105. procedure sizes2load(s1 : tcgsize;s2 : topsize; var op: tasmop; var s3: topsize);
  106. procedure floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  107. procedure floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  108. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  109. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  110. end;
  111. const
  112. TCGSize2OpSize: Array[tcgsize] of topsize =
  113. (S_NO,S_B,S_W,S_L,S_L,S_B,S_W,S_L,S_L,
  114. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  115. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  116. implementation
  117. uses
  118. globtype,globals,verbose,systems,cutils,
  119. symdef,symsym,defutil,paramgr,
  120. rgobj,tgobj,rgcpu;
  121. {$ifndef NOTARGETWIN32}
  122. const
  123. winstackpagesize = 4096;
  124. {$endif NOTARGETWIN32}
  125. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIV,
  126. A_IDIV,A_MUL, A_IMUL, A_NEG,A_NOT,A_OR,
  127. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  128. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  129. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  130. {****************************************************************************
  131. This is private property, keep out! :)
  132. ****************************************************************************}
  133. procedure tcgx86.sizes2load(s1 : tcgsize;s2: topsize; var op: tasmop; var s3: topsize);
  134. begin
  135. case s2 of
  136. S_B:
  137. if S1 in [OS_8,OS_S8] then
  138. s3 := S_B
  139. else internalerror(200109221);
  140. S_W:
  141. case s1 of
  142. OS_8,OS_S8:
  143. s3 := S_BW;
  144. OS_16,OS_S16:
  145. s3 := S_W;
  146. else internalerror(200109222);
  147. end;
  148. S_L:
  149. case s1 of
  150. OS_8,OS_S8:
  151. s3 := S_BL;
  152. OS_16,OS_S16:
  153. s3 := S_WL;
  154. OS_32,OS_S32:
  155. s3 := S_L;
  156. else internalerror(200109223);
  157. end;
  158. else internalerror(200109227);
  159. end;
  160. if s3 in [S_B,S_W,S_L] then
  161. op := A_MOV
  162. else if s1 in [OS_8,OS_16,OS_32] then
  163. op := A_MOVZX
  164. else
  165. op := A_MOVSX;
  166. end;
  167. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  168. begin
  169. case t of
  170. OS_F32 :
  171. begin
  172. op:=A_FLD;
  173. s:=S_FS;
  174. end;
  175. OS_F64 :
  176. begin
  177. op:=A_FLD;
  178. { ???? }
  179. s:=S_FL;
  180. end;
  181. OS_F80 :
  182. begin
  183. op:=A_FLD;
  184. s:=S_FX;
  185. end;
  186. OS_C64 :
  187. begin
  188. op:=A_FILD;
  189. s:=S_IQ;
  190. end;
  191. else
  192. internalerror(200204041);
  193. end;
  194. end;
  195. procedure tcgx86.floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  196. var
  197. op : tasmop;
  198. s : topsize;
  199. begin
  200. floatloadops(t,op,s);
  201. list.concat(Taicpu.Op_ref(op,s,ref));
  202. inc(trgcpu(rg).fpuvaroffset);
  203. end;
  204. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  205. begin
  206. case t of
  207. OS_F32 :
  208. begin
  209. op:=A_FSTP;
  210. s:=S_FS;
  211. end;
  212. OS_F64 :
  213. begin
  214. op:=A_FSTP;
  215. s:=S_FL;
  216. end;
  217. OS_F80 :
  218. begin
  219. op:=A_FSTP;
  220. s:=S_FX;
  221. end;
  222. OS_C64 :
  223. begin
  224. op:=A_FISTP;
  225. s:=S_IQ;
  226. end;
  227. else
  228. internalerror(200204042);
  229. end;
  230. end;
  231. procedure tcgx86.floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  232. var
  233. op : tasmop;
  234. s : topsize;
  235. begin
  236. floatstoreops(t,op,s);
  237. list.concat(Taicpu.Op_ref(op,s,ref));
  238. dec(trgcpu(rg).fpuvaroffset);
  239. end;
  240. {****************************************************************************
  241. Assembler code
  242. ****************************************************************************}
  243. class function tcgx86.reg_cgsize(const reg: tregister): tcgsize;
  244. const
  245. regsize_2_cgsize: array[S_B..S_L] of tcgsize = (OS_8,OS_16,OS_32);
  246. begin
  247. if reg.enum>lastreg then
  248. internalerror(200301081);
  249. result := regsize_2_cgsize[reg2opsize[reg.enum]];
  250. end;
  251. { currently does nothing }
  252. procedure tcgx86.a_jmp_always(list : taasmoutput;l: tasmlabel);
  253. begin
  254. a_jmp_cond(list, OC_NONE, l);
  255. end;
  256. { we implement the following routines because otherwise we can't }
  257. { instantiate the class since it's abstract }
  258. procedure tcgx86.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  259. begin
  260. case size of
  261. OS_8,OS_S8,
  262. OS_16,OS_S16:
  263. begin
  264. if target_info.alignment.paraalign = 2 then
  265. list.concat(taicpu.op_reg(A_PUSH,S_W,rg.makeregsize(r,OS_16)))
  266. else
  267. list.concat(taicpu.op_reg(A_PUSH,S_L,rg.makeregsize(r,OS_32)));
  268. end;
  269. OS_32,OS_S32:
  270. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  271. else
  272. internalerror(2002032212);
  273. end;
  274. end;
  275. procedure tcgx86.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  276. begin
  277. case size of
  278. OS_8,OS_S8,OS_16,OS_S16:
  279. begin
  280. if target_info.alignment.paraalign = 2 then
  281. list.concat(taicpu.op_const(A_PUSH,S_W,a))
  282. else
  283. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  284. end;
  285. OS_32,OS_S32:
  286. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  287. else
  288. internalerror(2002032213);
  289. end;
  290. end;
  291. procedure tcgx86.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  292. var
  293. tmpreg: tregister;
  294. begin
  295. case size of
  296. OS_8,OS_S8,
  297. OS_16,OS_S16:
  298. begin
  299. tmpreg := get_scratch_reg_address(list);
  300. a_load_ref_reg(list,size,r,tmpreg);
  301. if target_info.alignment.paraalign = 2 then
  302. list.concat(taicpu.op_reg(A_PUSH,S_W,rg.makeregsize(tmpreg,OS_16)))
  303. else
  304. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  305. free_scratch_reg(list,tmpreg);
  306. end;
  307. OS_32,OS_S32:
  308. list.concat(taicpu.op_ref(A_PUSH,S_L,r));
  309. else
  310. internalerror(2002032214);
  311. end;
  312. end;
  313. procedure tcgx86.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  314. var
  315. tmpreg: tregister;
  316. begin
  317. if r.segment.enum<>R_NO then
  318. CGMessage(cg_e_cant_use_far_pointer_there);
  319. if r.base.enum>lastreg then
  320. internalerror(200301081);
  321. if r.index.enum>lastreg then
  322. internalerror(200301081);
  323. if (r.base.enum=R_NO) and (r.index.enum=R_NO) then
  324. begin
  325. if assigned(r.symbol) then
  326. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,r.symbol,r.offset))
  327. else
  328. list.concat(Taicpu.Op_const(A_PUSH,S_L,r.offset));
  329. end
  330. else if (r.base.enum=R_NO) and (r.index.enum<>R_NO) and
  331. (r.offset=0) and (r.scalefactor=0) and (r.symbol=nil) then
  332. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.index))
  333. else if (r.base.enum<>R_NO) and (r.index.enum=R_NO) and
  334. (r.offset=0) and (r.symbol=nil) then
  335. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.base))
  336. else
  337. begin
  338. tmpreg := get_scratch_reg_address(list);
  339. a_loadaddr_ref_reg(list,r,tmpreg);
  340. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  341. free_scratch_reg(list,tmpreg);
  342. end;
  343. end;
  344. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  345. begin
  346. list.concat(taicpu.op_sym(A_CALL,S_NO,objectlibrary.newasmsymbol(s)));
  347. end;
  348. procedure tcgx86.a_call_ref(list : taasmoutput;const ref : treference);
  349. begin
  350. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  351. end;
  352. procedure tcgx86.a_call_reg(list : taasmoutput;reg : tregister);
  353. begin
  354. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  355. end;
  356. {********************** load instructions ********************}
  357. procedure tcgx86.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  358. begin
  359. { the optimizer will change it to "xor reg,reg" when loading zero, }
  360. { no need to do it here too (JM) }
  361. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[size],a,reg))
  362. end;
  363. procedure tcgx86.a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);
  364. begin
  365. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[size],a,ref));
  366. end;
  367. procedure tcgx86.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  368. begin
  369. list.concat(taicpu.op_reg_ref(A_MOV,TCGSize2OpSize[size],reg,
  370. ref));
  371. End;
  372. procedure tcgx86.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  373. var
  374. op: tasmop;
  375. o,s: topsize;
  376. begin
  377. if reg.enum=R_INTREGISTER then
  378. o:=subreg2opsize[reg.number and $ff]
  379. else
  380. o:=reg2opsize[reg.enum];
  381. sizes2load(size,o,op,s);
  382. list.concat(taicpu.op_ref_reg(op,s,ref,reg));
  383. end;
  384. procedure tcgx86.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  385. var
  386. op: tasmop;
  387. s: topsize;
  388. begin
  389. sizes2load(fromsize,reg2opsize[reg2.enum],op,s);
  390. if (rg.makeregsize(reg1,OS_INT).enum = rg.makeregsize(reg2,OS_INT).enum) then
  391. begin
  392. { "mov reg1, reg1" doesn't make sense }
  393. if op = A_MOV then
  394. exit;
  395. { optimize movzx with "and ffff,<reg>" operation }
  396. if (op = A_MOVZX) then
  397. begin
  398. case fromsize of
  399. OS_8:
  400. begin
  401. list.concat(taicpu.op_const_reg(A_AND,reg2opsize[reg2.enum],255,reg2));
  402. exit;
  403. end;
  404. OS_16:
  405. begin
  406. list.concat(taicpu.op_const_reg(A_AND,reg2opsize[reg2.enum],65535,reg2));
  407. exit;
  408. end;
  409. end;
  410. end;
  411. end;
  412. list.concat(taicpu.op_reg_reg(op,s,reg1,reg2));
  413. end;
  414. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  415. begin
  416. if assigned(ref.symbol) and
  417. (ref.base.enum=R_NO) and
  418. (ref.index.enum=R_NO) then
  419. list.concat(taicpu.op_sym_ofs_reg(A_MOV,S_L,ref.symbol,ref.offset,r))
  420. else
  421. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,r));
  422. end;
  423. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  424. { R_ST means "the current value at the top of the fpu stack" (JM) }
  425. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  426. begin
  427. if (reg1.enum <> R_ST) then
  428. begin
  429. list.concat(taicpu.op_reg(A_FLD,S_NO,
  430. trgcpu(rg).correct_fpuregister(reg1,trgcpu(rg).fpuvaroffset)));
  431. inc(trgcpu(rg).fpuvaroffset);
  432. end;
  433. if (reg2.enum <> R_ST) then
  434. begin
  435. list.concat(taicpu.op_reg(A_FSTP,S_NO,
  436. trgcpu(rg).correct_fpuregister(reg2,trgcpu(rg).fpuvaroffset)));
  437. dec(trgcpu(rg).fpuvaroffset);
  438. end;
  439. end;
  440. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  441. var rst:Tregister;
  442. begin
  443. rst.enum:=R_ST;
  444. floatload(list,size,ref);
  445. if reg.enum>lastreg then
  446. internalerror(200301081);
  447. if (reg.enum <> R_ST) then
  448. a_loadfpu_reg_reg(list,rst,reg);
  449. end;
  450. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  451. var rst:Tregister;
  452. begin
  453. rst.enum:=R_ST;
  454. if reg.enum>lastreg then
  455. internalerror(200301081);
  456. if reg.enum <> R_ST then
  457. a_loadfpu_reg_reg(list,reg,rst);
  458. floatstore(list,size,ref);
  459. end;
  460. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  461. begin
  462. list.concat(taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2));
  463. end;
  464. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister);
  465. begin
  466. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,ref,reg));
  467. end;
  468. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference);
  469. begin
  470. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,ref));
  471. end;
  472. procedure tcgx86.a_parammm_reg(list: taasmoutput; reg: tregister);
  473. var
  474. href : treference;
  475. r : Tregister;
  476. begin
  477. r.enum:=R_ESP;
  478. list.concat(taicpu.op_const_reg(A_SUB,S_L,8,r));
  479. reference_reset_base(href,r,0);
  480. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,href));
  481. end;
  482. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  483. var
  484. opcode: tasmop;
  485. power: longint;
  486. begin
  487. if reg.enum>lastreg then
  488. internalerror(200301081);
  489. Case Op of
  490. OP_DIV, OP_IDIV:
  491. Begin
  492. if ispowerof2(a,power) then
  493. begin
  494. case op of
  495. OP_DIV:
  496. opcode := A_SHR;
  497. OP_IDIV:
  498. opcode := A_SAR;
  499. end;
  500. list.concat(taicpu.op_const_reg(opcode,reg2opsize[reg.enum],power,
  501. reg));
  502. exit;
  503. end;
  504. { the rest should be handled specifically in the code }
  505. { generator because of the silly register usage restraints }
  506. internalerror(200109224);
  507. End;
  508. OP_MUL,OP_IMUL:
  509. begin
  510. if not(cs_check_overflow in aktlocalswitches) and
  511. ispowerof2(a,power) then
  512. begin
  513. list.concat(taicpu.op_const_reg(A_SHL,reg2opsize[reg.enum],power,
  514. reg));
  515. exit;
  516. end;
  517. if op = OP_IMUL then
  518. list.concat(taicpu.op_const_reg(A_IMUL,reg2opsize[reg.enum],
  519. a,reg))
  520. else
  521. { OP_MUL should be handled specifically in the code }
  522. { generator because of the silly register usage restraints }
  523. internalerror(200109225);
  524. end;
  525. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  526. if not(cs_check_overflow in aktlocalswitches) and
  527. (a = 1) and
  528. (op in [OP_ADD,OP_SUB]) then
  529. if op = OP_ADD then
  530. list.concat(taicpu.op_reg(A_INC,reg2opsize[reg.enum],reg))
  531. else
  532. list.concat(taicpu.op_reg(A_DEC,reg2opsize[reg.enum],reg))
  533. else if (a = 0) then
  534. if (op <> OP_AND) then
  535. exit
  536. else
  537. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize[reg.enum],0,reg))
  538. else if (a = high(aword)) and
  539. (op in [OP_AND,OP_OR,OP_XOR]) then
  540. begin
  541. case op of
  542. OP_AND:
  543. exit;
  544. OP_OR:
  545. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize[reg.enum],high(aword),reg));
  546. OP_XOR:
  547. list.concat(taicpu.op_reg(A_NOT,reg2opsize[reg.enum],reg));
  548. end
  549. end
  550. else
  551. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],reg2opsize[reg.enum],
  552. a,reg));
  553. OP_SHL,OP_SHR,OP_SAR:
  554. begin
  555. if (a and 31) <> 0 Then
  556. list.concat(taicpu.op_const_reg(
  557. TOpCG2AsmOp[op],reg2opsize[reg.enum],a and 31,reg));
  558. if (a shr 5) <> 0 Then
  559. internalerror(68991);
  560. end
  561. else internalerror(68992);
  562. end;
  563. end;
  564. procedure tcgx86.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  565. var
  566. opcode: tasmop;
  567. power: longint;
  568. begin
  569. Case Op of
  570. OP_DIV, OP_IDIV:
  571. Begin
  572. if ispowerof2(a,power) then
  573. begin
  574. case op of
  575. OP_DIV:
  576. opcode := A_SHR;
  577. OP_IDIV:
  578. opcode := A_SAR;
  579. end;
  580. list.concat(taicpu.op_const_ref(opcode,
  581. TCgSize2OpSize[size],power,ref));
  582. exit;
  583. end;
  584. { the rest should be handled specifically in the code }
  585. { generator because of the silly register usage restraints }
  586. internalerror(200109231);
  587. End;
  588. OP_MUL,OP_IMUL:
  589. begin
  590. if not(cs_check_overflow in aktlocalswitches) and
  591. ispowerof2(a,power) then
  592. begin
  593. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  594. power,ref));
  595. exit;
  596. end;
  597. { can't multiply a memory location directly with a constant }
  598. if op = OP_IMUL then
  599. inherited a_op_const_ref(list,op,size,a,ref)
  600. else
  601. { OP_MUL should be handled specifically in the code }
  602. { generator because of the silly register usage restraints }
  603. internalerror(200109232);
  604. end;
  605. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  606. if not(cs_check_overflow in aktlocalswitches) and
  607. (a = 1) and
  608. (op in [OP_ADD,OP_SUB]) then
  609. if op = OP_ADD then
  610. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],ref))
  611. else
  612. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],ref))
  613. else if (a = 0) then
  614. if (op <> OP_AND) then
  615. exit
  616. else
  617. a_load_const_ref(list,size,0,ref)
  618. else if (a = high(aword)) and
  619. (op in [OP_AND,OP_OR,OP_XOR]) then
  620. begin
  621. case op of
  622. OP_AND:
  623. exit;
  624. OP_OR:
  625. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],high(aword),ref));
  626. OP_XOR:
  627. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],ref));
  628. end
  629. end
  630. else
  631. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  632. TCgSize2OpSize[size],a,ref));
  633. OP_SHL,OP_SHR,OP_SAR:
  634. begin
  635. if (a and 31) <> 0 Then
  636. list.concat(taicpu.op_const_ref(
  637. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,ref));
  638. if (a shr 5) <> 0 Then
  639. internalerror(68991);
  640. end
  641. else internalerror(68992);
  642. end;
  643. end;
  644. procedure tcgx86.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  645. var
  646. regloadsize: tcgsize;
  647. dstsize: topsize;
  648. tmpreg : tregister;
  649. popecx : boolean;
  650. r:Tregister;
  651. begin
  652. if src.enum>lastreg then
  653. internalerror(200301081);
  654. if dst.enum>lastreg then
  655. internalerror(200301081);
  656. dstsize := tcgsize2opsize[size];
  657. dst := rg.makeregsize(dst,size);
  658. case op of
  659. OP_NEG,OP_NOT:
  660. begin
  661. if src.enum <> R_NO then
  662. internalerror(200112291);
  663. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  664. end;
  665. OP_MUL,OP_DIV,OP_IDIV:
  666. { special stuff, needs separate handling inside code }
  667. { generator }
  668. internalerror(200109233);
  669. OP_SHR,OP_SHL,OP_SAR:
  670. begin
  671. tmpreg.enum := R_NO;
  672. popecx := false;
  673. { we need cl to hold the shift count, so if the destination }
  674. { is ecx, save it to a temp for now }
  675. if dst.enum in [R_ECX,R_CX,R_CL] then
  676. begin
  677. case reg2opsize[dst.enum] of
  678. S_B: regloadsize := OS_8;
  679. S_W: regloadsize := OS_16;
  680. else regloadsize := OS_32;
  681. end;
  682. tmpreg := get_scratch_reg_int(list);
  683. a_load_reg_reg(list,regloadsize,regloadsize,src,tmpreg);
  684. end;
  685. if not(src.enum in [R_ECX,R_CX,R_CL]) then
  686. begin
  687. { is ecx still free (it's also free if it was allocated }
  688. { to dst, since we've moved dst somewhere else already) }
  689. if not((dst.enum = R_ECX) or
  690. ((R_ECX in rg.unusedregsint) and
  691. { this will always be true, it's just here to }
  692. { allocate ecx }
  693. (rg.getexplicitregisterint(list,R_ECX).enum = R_ECX))) then
  694. begin
  695. r.enum:=R_ECX;
  696. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  697. popecx := true;
  698. end;
  699. r.enum:=R_ECX;
  700. a_load_reg_reg(list,OS_32,OS_32,rg.makeregsize(src,OS_32),r);
  701. end
  702. else
  703. src.enum := R_CL;
  704. { do the shift }
  705. r.enum:=R_CL;
  706. if tmpreg.enum = R_NO then
  707. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  708. r,dst))
  709. else
  710. begin
  711. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],S_L,
  712. r,tmpreg));
  713. { move result back to the destination }
  714. r.enum:=R_ECX;
  715. a_load_reg_reg(list,OS_32,OS_32,tmpreg,r);
  716. free_scratch_reg(list,tmpreg);
  717. end;
  718. r.enum:=R_ECX;
  719. if popecx then
  720. list.concat(taicpu.op_reg(A_POP,S_L,r))
  721. else if not (dst.enum in [R_ECX,R_CX,R_CL]) then
  722. rg.ungetregisterint(list,r);
  723. end;
  724. else
  725. begin
  726. if reg2opsize[src.enum] <> dstsize then
  727. internalerror(200109226);
  728. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  729. src,dst));
  730. end;
  731. end;
  732. end;
  733. procedure tcgx86.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  734. begin
  735. case op of
  736. OP_NEG,OP_NOT,OP_IMUL:
  737. begin
  738. inherited a_op_ref_reg(list,op,size,ref,reg);
  739. end;
  740. OP_MUL,OP_DIV,OP_IDIV:
  741. { special stuff, needs separate handling inside code }
  742. { generator }
  743. internalerror(200109239);
  744. else
  745. begin
  746. reg := rg.makeregsize(reg,size);
  747. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],ref,reg));
  748. end;
  749. end;
  750. end;
  751. procedure tcgx86.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  752. var
  753. opsize: topsize;
  754. begin
  755. if reg.enum>lastreg then
  756. internalerror(200201081);
  757. case op of
  758. OP_NEG,OP_NOT:
  759. begin
  760. if reg.enum <> R_NO then
  761. internalerror(200109237);
  762. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],ref));
  763. end;
  764. OP_IMUL:
  765. begin
  766. { this one needs a load/imul/store, which is the default }
  767. inherited a_op_ref_reg(list,op,size,ref,reg);
  768. end;
  769. OP_MUL,OP_DIV,OP_IDIV:
  770. { special stuff, needs separate handling inside code }
  771. { generator }
  772. internalerror(200109238);
  773. else
  774. begin
  775. opsize := tcgsize2opsize[size];
  776. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],opsize,reg,ref));
  777. end;
  778. end;
  779. end;
  780. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  781. size: tcgsize; a: aword; src, dst: tregister);
  782. var
  783. tmpref: treference;
  784. power: longint;
  785. opsize: topsize;
  786. begin
  787. if src.enum>lastreg then
  788. internalerror(200201081);
  789. if dst.enum>lastreg then
  790. internalerror(200201081);
  791. opsize := reg2opsize[src.enum];
  792. if (opsize <> S_L) or
  793. not (size in [OS_32,OS_S32]) then
  794. begin
  795. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  796. exit;
  797. end;
  798. { if we get here, we have to do a 32 bit calculation, guaranteed }
  799. Case Op of
  800. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  801. OP_SAR:
  802. { can't do anything special for these }
  803. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  804. OP_IMUL:
  805. begin
  806. if not(cs_check_overflow in aktlocalswitches) and
  807. ispowerof2(a,power) then
  808. { can be done with a shift }
  809. begin
  810. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  811. exit;
  812. end;
  813. list.concat(taicpu.op_const_reg_reg(A_IMUL,S_L,a,src,dst));
  814. end;
  815. OP_ADD, OP_SUB:
  816. if (a = 0) then
  817. a_load_reg_reg(list,size,size,src,dst)
  818. else
  819. begin
  820. reference_reset(tmpref);
  821. tmpref.base := src;
  822. tmpref.offset := longint(a);
  823. if op = OP_SUB then
  824. tmpref.offset := -tmpref.offset;
  825. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  826. end
  827. else internalerror(200112302);
  828. end;
  829. end;
  830. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  831. size: tcgsize; src1, src2, dst: tregister);
  832. var
  833. tmpref: treference;
  834. opsize: topsize;
  835. begin
  836. if src1.enum>lastreg then
  837. internalerror(200201081);
  838. if src2.enum>lastreg then
  839. internalerror(200201081);
  840. if dst.enum>lastreg then
  841. internalerror(200201081);
  842. opsize := reg2opsize[src1.enum];
  843. if (opsize <> S_L) or
  844. (reg2opsize[src2.enum] <> S_L) or
  845. not (size in [OS_32,OS_S32]) then
  846. begin
  847. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  848. exit;
  849. end;
  850. { if we get here, we have to do a 32 bit calculation, guaranteed }
  851. Case Op of
  852. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  853. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  854. { can't do anything special for these }
  855. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  856. OP_IMUL:
  857. list.concat(taicpu.op_reg_reg_reg(A_IMUL,S_L,src1,src2,dst));
  858. OP_ADD:
  859. begin
  860. reference_reset(tmpref);
  861. tmpref.base := src1;
  862. tmpref.index := src2;
  863. tmpref.scalefactor := 1;
  864. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  865. end
  866. else internalerror(200112303);
  867. end;
  868. end;
  869. {*************** compare instructructions ****************}
  870. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  871. l : tasmlabel);
  872. begin
  873. if reg.enum>lastreg then
  874. internalerror(200101081);
  875. if (a = 0) then
  876. list.concat(taicpu.op_reg_reg(A_TEST,reg2opsize[reg.enum],reg,reg))
  877. else
  878. list.concat(taicpu.op_const_reg(A_CMP,reg2opsize[reg.enum],a,reg));
  879. a_jmp_cond(list,cmp_op,l);
  880. end;
  881. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  882. l : tasmlabel);
  883. begin
  884. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,ref));
  885. a_jmp_cond(list,cmp_op,l);
  886. end;
  887. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  888. reg1,reg2 : tregister;l : tasmlabel);
  889. begin
  890. if reg1.enum>lastreg then
  891. internalerror(200101081);
  892. if reg2.enum>lastreg then
  893. internalerror(200101081);
  894. if reg2opsize[reg1.enum] <> reg2opsize[reg2.enum] then
  895. internalerror(200109226);
  896. list.concat(taicpu.op_reg_reg(A_CMP,reg2opsize[reg1.enum],reg1,reg2));
  897. a_jmp_cond(list,cmp_op,l);
  898. end;
  899. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  900. begin
  901. reg := rg.makeregsize(reg,size);
  902. list.concat(taicpu.op_ref_reg(A_CMP,tcgsize2opsize[size],ref,reg));
  903. a_jmp_cond(list,cmp_op,l);
  904. end;
  905. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  906. var
  907. ai : taicpu;
  908. begin
  909. if cond=OC_None then
  910. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  911. else
  912. begin
  913. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  914. ai.SetCondition(TOpCmp2AsmCond[cond]);
  915. end;
  916. ai.is_jmp:=true;
  917. list.concat(ai);
  918. end;
  919. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  920. var
  921. ai : taicpu;
  922. begin
  923. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  924. ai.SetCondition(flags_to_cond(f));
  925. ai.is_jmp := true;
  926. list.concat(ai);
  927. end;
  928. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  929. var
  930. ai : taicpu;
  931. hreg : tregister;
  932. begin
  933. if reg.enum>lastreg then
  934. internalerror(200201081);
  935. hreg := rg.makeregsize(reg,OS_8);
  936. ai:=Taicpu.Op_reg(A_Setcc,S_B,hreg);
  937. ai.SetCondition(flags_to_cond(f));
  938. list.concat(ai);
  939. if (reg.enum <> hreg.enum) then
  940. a_load_reg_reg(list,OS_8,size,hreg,reg);
  941. end;
  942. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  943. var
  944. ai : taicpu;
  945. begin
  946. if not(size in [OS_8,OS_S8]) then
  947. a_load_const_ref(list,size,0,ref);
  948. ai:=Taicpu.Op_ref(A_Setcc,S_B,ref);
  949. ai.SetCondition(flags_to_cond(f));
  950. list.concat(ai);
  951. end;
  952. { ************* concatcopy ************ }
  953. procedure tcgx86.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  954. var
  955. ecxpushed : boolean;
  956. helpsize : longint;
  957. i : byte;
  958. reg8,reg32 : tregister;
  959. srcref,dstref : treference;
  960. swap : boolean;
  961. r : Tregister;
  962. procedure maybepushecx;
  963. var r:Tregister;
  964. begin
  965. r.enum:=R_ECX;
  966. if not(R_ECX in rg.unusedregsint) then
  967. begin
  968. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  969. ecxpushed:=true;
  970. end
  971. else rg.getexplicitregisterint(list,R_ECX);
  972. end;
  973. begin
  974. if (not loadref) and
  975. ((len<=8) or
  976. (not(cs_littlesize in aktglobalswitches ) and (len<=12))) then
  977. begin
  978. helpsize:=len shr 2;
  979. rg.getexplicitregisterint(list,R_EDI);
  980. dstref:=dest;
  981. srcref:=source;
  982. for i:=1 to helpsize do
  983. begin
  984. r.enum:=R_EDI;
  985. a_load_ref_reg(list,OS_32,srcref,r);
  986. If (len = 4) and delsource then
  987. reference_release(list,source);
  988. a_load_reg_ref(list,OS_32,r,dstref);
  989. inc(srcref.offset,4);
  990. inc(dstref.offset,4);
  991. dec(len,4);
  992. end;
  993. if len>1 then
  994. begin
  995. r.enum:=R_DI;
  996. a_load_ref_reg(list,OS_16,srcref,r);
  997. If (len = 2) and delsource then
  998. reference_release(list,source);
  999. a_load_reg_ref(list,OS_16,r,dstref);
  1000. inc(srcref.offset,2);
  1001. inc(dstref.offset,2);
  1002. dec(len,2);
  1003. end;
  1004. r.enum:=R_EDI;
  1005. rg.ungetregisterint(list,r);
  1006. if len>0 then
  1007. begin
  1008. { and now look for an 8 bit register }
  1009. swap:=false;
  1010. if R_EAX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EAX),OS_8)
  1011. else if R_EDX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EDX),OS_8)
  1012. else if R_EBX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EBX),OS_8)
  1013. else if R_ECX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_ECX),OS_8)
  1014. else
  1015. begin
  1016. swap:=true;
  1017. { we need only to check 3 registers, because }
  1018. { one is always not index or base }
  1019. if (dest.base.enum<>R_EAX) and (dest.index.enum<>R_EAX) then
  1020. begin
  1021. reg8.enum:=R_AL;
  1022. reg32.enum:=R_EAX;
  1023. end
  1024. else if (dest.base.enum<>R_EBX) and (dest.index.enum<>R_EBX) then
  1025. begin
  1026. reg8.enum:=R_BL;
  1027. reg32.enum:=R_EBX;
  1028. end
  1029. else if (dest.base.enum<>R_ECX) and (dest.index.enum<>R_ECX) then
  1030. begin
  1031. reg8.enum:=R_CL;
  1032. reg32.enum:=R_ECX;
  1033. end;
  1034. end;
  1035. if swap then
  1036. { was earlier XCHG, of course nonsense }
  1037. begin
  1038. rg.getexplicitregisterint(list,R_EDI);
  1039. r.enum:=R_EDI;
  1040. a_load_reg_reg(list,OS_32,OS_32,reg32,r);
  1041. end;
  1042. a_load_ref_reg(list,OS_8,srcref,reg8);
  1043. If delsource and (len=1) then
  1044. reference_release(list,source);
  1045. a_load_reg_ref(list,OS_8,reg8,dstref);
  1046. if swap then
  1047. begin
  1048. r.enum:=R_EDI;
  1049. a_load_reg_reg(list,OS_32,OS_32,r,reg32);
  1050. rg.ungetregisterint(list,r);
  1051. end
  1052. else
  1053. rg.ungetregister(list,reg8);
  1054. end;
  1055. end
  1056. else
  1057. begin
  1058. r.enum:=R_EDI;
  1059. rg.getexplicitregisterint(list,R_EDI);
  1060. a_loadaddr_ref_reg(list,dest,r);
  1061. r.enum:=R_ESI;
  1062. list.concat(tai_regalloc.Alloc(r));
  1063. if loadref then
  1064. a_load_ref_reg(list,OS_ADDR,source,r)
  1065. else
  1066. begin
  1067. a_loadaddr_ref_reg(list,source,r);
  1068. if delsource then
  1069. reference_release(list,source);
  1070. end;
  1071. list.concat(Taicpu.Op_none(A_CLD,S_NO));
  1072. ecxpushed:=false;
  1073. r.enum:=R_ECX;
  1074. if cs_littlesize in aktglobalswitches then
  1075. begin
  1076. maybepushecx;
  1077. a_load_const_reg(list,OS_INT,len,r);
  1078. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1079. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1080. end
  1081. else
  1082. begin
  1083. helpsize:=len shr 2;
  1084. len:=len and 3;
  1085. if helpsize>1 then
  1086. begin
  1087. maybepushecx;
  1088. a_load_const_reg(list,OS_INT,helpsize,r);
  1089. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1090. end;
  1091. if helpsize>0 then
  1092. list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1093. if len>1 then
  1094. begin
  1095. dec(len,2);
  1096. list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1097. end;
  1098. if len=1 then
  1099. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1100. end;
  1101. r.enum:=R_EDI;
  1102. rg.ungetregisterint(list,r);
  1103. r.enum:=R_ESI;
  1104. list.concat(tai_regalloc.DeAlloc(r));
  1105. r.enum:=R_ECX;
  1106. if ecxpushed then
  1107. list.concat(Taicpu.Op_reg(A_POP,S_L,r))
  1108. else
  1109. rg.ungetregisterint(list,r);
  1110. { loading SELF-reference again }
  1111. g_maybe_loadself(list);
  1112. end;
  1113. if delsource then
  1114. tg.ungetiftemp(list,source);
  1115. end;
  1116. procedure tcgx86.g_exception_reason_save(list : taasmoutput; const href : treference);
  1117. var r:Tregister;
  1118. begin
  1119. r.enum:=R_EAX;
  1120. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1121. end;
  1122. procedure tcgx86.g_exception_reason_save_const(list : taasmoutput;const href : treference; a: aword);
  1123. begin
  1124. list.concat(Taicpu.op_const(A_PUSH,S_L,a));
  1125. end;
  1126. procedure tcgx86.g_exception_reason_load(list : taasmoutput; const href : treference);
  1127. var r:Tregister;
  1128. begin
  1129. r.enum:=R_EAX;
  1130. list.concat(Taicpu.op_reg(A_POP,S_L,r));
  1131. end;
  1132. {****************************************************************************
  1133. Entry/Exit Code Helpers
  1134. ****************************************************************************}
  1135. procedure tcgx86.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1136. var
  1137. lenref : treference;
  1138. power,len : longint;
  1139. opsize : topsize;
  1140. {$ifndef __NOWINPECOFF__}
  1141. again,ok : tasmlabel;
  1142. {$endif}
  1143. r,r2,rsp:Tregister;
  1144. begin
  1145. lenref:=ref;
  1146. inc(lenref.offset,4);
  1147. { get stack space }
  1148. r.enum:=R_EDI;
  1149. rsp.enum:=R_ESP;
  1150. rg.getexplicitregisterint(list,R_EDI);
  1151. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1152. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1153. if (elesize<>1) then
  1154. begin
  1155. if ispowerof2(elesize, power) then
  1156. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1157. else
  1158. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1159. end;
  1160. {$ifndef __NOWINPECOFF__}
  1161. { windows guards only a few pages for stack growing, }
  1162. { so we have to access every page first }
  1163. if target_info.system=system_i386_win32 then
  1164. begin
  1165. objectlibrary.getlabel(again);
  1166. objectlibrary.getlabel(ok);
  1167. a_label(list,again);
  1168. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1169. a_jmp_cond(list,OC_B,ok);
  1170. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1171. r2.enum:=R_EAX;
  1172. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1173. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1174. a_jmp_always(list,again);
  1175. a_label(list,ok);
  1176. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1177. rg.ungetregisterint(list,r);
  1178. { now reload EDI }
  1179. rg.getexplicitregisterint(list,R_EDI);
  1180. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1181. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1182. if (elesize<>1) then
  1183. begin
  1184. if ispowerof2(elesize, power) then
  1185. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1186. else
  1187. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1188. end;
  1189. end
  1190. else
  1191. {$endif __NOWINPECOFF__}
  1192. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1193. { align stack on 4 bytes }
  1194. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1195. { load destination }
  1196. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,rsp,r));
  1197. { don't destroy the registers! }
  1198. r2.enum:=R_ECX;
  1199. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1200. r2.enum:=R_ESI;
  1201. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1202. { load count }
  1203. r2.enum:=R_ECX;
  1204. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r2));
  1205. { load source }
  1206. r2.enum:=R_ESI;
  1207. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,ref,r2));
  1208. { scheduled .... }
  1209. r2.enum:=R_ECX;
  1210. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1211. { calculate size }
  1212. len:=elesize;
  1213. opsize:=S_B;
  1214. if (len and 3)=0 then
  1215. begin
  1216. opsize:=S_L;
  1217. len:=len shr 2;
  1218. end
  1219. else
  1220. if (len and 1)=0 then
  1221. begin
  1222. opsize:=S_W;
  1223. len:=len shr 1;
  1224. end;
  1225. if ispowerof2(len, power) then
  1226. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1227. else
  1228. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1229. list.concat(Taicpu.op_none(A_REP,S_NO));
  1230. case opsize of
  1231. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1232. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1233. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1234. end;
  1235. rg.ungetregisterint(list,r);
  1236. r2.enum:=R_ESI;
  1237. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1238. r2.enum:=R_ECX;
  1239. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1240. { patch the new address }
  1241. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,rsp,ref));
  1242. end;
  1243. procedure tcgx86.g_removevaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1244. var
  1245. lenref : treference;
  1246. power,len : longint;
  1247. r,rsp:Tregister;
  1248. begin
  1249. lenref:=ref;
  1250. inc(lenref.offset,4);
  1251. { caluclate size and adjust stack space }
  1252. rg.getexplicitregisterint(list,R_EDI);
  1253. r.enum:=R_EDI;
  1254. rsp.enum:=R_ESP;
  1255. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1256. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1257. if (elesize<>1) then
  1258. begin
  1259. if ispowerof2(elesize, power) then
  1260. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1261. else
  1262. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1263. end;
  1264. list.concat(Taicpu.op_reg_reg(A_ADD,S_L,r,rsp));
  1265. end;
  1266. procedure tcgx86.g_interrupt_stackframe_entry(list : taasmoutput);
  1267. var r:Tregister;
  1268. begin
  1269. r.enum:=R_INTREGISTER;
  1270. r.enum:=R_GS;
  1271. { .... also the segment registers }
  1272. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1273. r.enum:=R_FS;
  1274. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1275. r.enum:=R_ES;
  1276. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1277. r.enum:=R_DS;
  1278. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1279. { save the registers of an interrupt procedure }
  1280. r.enum:=R_EDI;
  1281. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1282. r.enum:=R_ESI;
  1283. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1284. r.enum:=R_EDX;
  1285. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1286. r.enum:=R_ECX;
  1287. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1288. r.enum:=R_EBX;
  1289. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1290. r.enum:=R_EAX;
  1291. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1292. end;
  1293. procedure tcgx86.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1294. var r:Tregister;
  1295. begin
  1296. r.enum:=R_INTREGISTER;
  1297. if accused then
  1298. begin
  1299. r.number:=NR_ESP;
  1300. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1301. end
  1302. else
  1303. begin
  1304. r.number:=NR_EAX;
  1305. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1306. end;
  1307. r.number:=NR_EBX;
  1308. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1309. r.number:=NR_ECX;
  1310. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1311. if acchiused then
  1312. begin
  1313. r.number:=NR_ESP;
  1314. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1315. end
  1316. else
  1317. begin
  1318. r.number:=NR_EDX;
  1319. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1320. end;
  1321. if selfused then
  1322. begin
  1323. r.number:=NR_ESP;
  1324. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1325. end
  1326. else
  1327. begin
  1328. r.number:=NR_ESI;
  1329. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1330. end;
  1331. r.number:=NR_EDI;
  1332. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1333. { .... also the segment registers }
  1334. r.number:=NR_DS;
  1335. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1336. r.number:=NR_ES;
  1337. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1338. r.number:=NR_FS;
  1339. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1340. r.number:=NR_GS;
  1341. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1342. { this restores the flags }
  1343. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  1344. end;
  1345. procedure tcgx86.g_profilecode(list : taasmoutput);
  1346. var
  1347. pl : tasmlabel;
  1348. r : Tregister;
  1349. begin
  1350. case target_info.system of
  1351. system_i386_win32,
  1352. system_i386_freebsd,
  1353. system_i386_wdosx,
  1354. system_i386_linux:
  1355. begin
  1356. objectlibrary.getaddrlabel(pl);
  1357. list.concat(Tai_section.Create(sec_data));
  1358. list.concat(Tai_align.Create(4));
  1359. list.concat(Tai_label.Create(pl));
  1360. list.concat(Tai_const.Create_32bit(0));
  1361. list.concat(Tai_section.Create(sec_code));
  1362. r.enum:=R_INTREGISTER;
  1363. r.number:=NR_EDX;
  1364. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,r));
  1365. a_call_name(list,target_info.Cprefix+'mcount');
  1366. include(rg.usedinproc,R_EDX);
  1367. end;
  1368. system_i386_go32v2:
  1369. begin
  1370. a_call_name(list,'MCOUNT');
  1371. end;
  1372. end;
  1373. end;
  1374. procedure tcgx86.g_stackpointer_alloc(list : taasmoutput;localsize : longint);
  1375. var
  1376. href : treference;
  1377. i : integer;
  1378. again : tasmlabel;
  1379. r,rsp : Tregister;
  1380. begin
  1381. r.enum:=R_INTREGISTER;
  1382. rsp.enum:=R_INTREGISTER;
  1383. rsp.number:=NR_ESP;
  1384. if localsize>0 then
  1385. begin
  1386. {$ifndef NOTARGETWIN32}
  1387. { windows guards only a few pages for stack growing, }
  1388. { so we have to access every page first }
  1389. if (target_info.system=system_i386_win32) and
  1390. (localsize>=winstackpagesize) then
  1391. begin
  1392. if localsize div winstackpagesize<=5 then
  1393. begin
  1394. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,rsp));
  1395. for i:=1 to localsize div winstackpagesize do
  1396. begin
  1397. reference_reset_base(href,rsp,localsize-i*winstackpagesize);
  1398. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1399. end;
  1400. r.number:=NR_EAX;
  1401. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1402. end
  1403. else
  1404. begin
  1405. objectlibrary.getlabel(again);
  1406. r.number:=NR_EDI;
  1407. rg.getexplicitregisterint(list,R_EDI);
  1408. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,r));
  1409. a_label(list,again);
  1410. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1411. r.number:=NR_EAX;
  1412. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1413. r.number:=NR_EDI;
  1414. list.concat(Taicpu.op_reg(A_DEC,S_L,r));
  1415. a_jmp_cond(list,OC_NE,again);
  1416. rg.ungetregisterint(list,r);
  1417. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize,rsp));
  1418. end
  1419. end
  1420. else
  1421. {$endif NOTARGETWIN32}
  1422. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize,rsp));
  1423. end;
  1424. end;
  1425. procedure tcgx86.g_stackframe_entry(list : taasmoutput;localsize : longint);
  1426. var r,rsp:Tregister;
  1427. begin
  1428. r.enum:=R_INTREGISTER;
  1429. r.number:=NR_EBP;
  1430. rsp.enum:=R_INTREGISTER;
  1431. rsp.number:=NR_ESP;
  1432. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1433. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,rsp,r));
  1434. if localsize>0 then
  1435. g_stackpointer_alloc(list,localsize);
  1436. end;
  1437. procedure tcgx86.g_restore_frame_pointer(list : taasmoutput);
  1438. begin
  1439. list.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  1440. end;
  1441. procedure tcgx86.g_return_from_proc(list : taasmoutput;parasize : aword);
  1442. begin
  1443. { Routines with the poclearstack flag set use only a ret }
  1444. { also routines with parasize=0 }
  1445. if (po_clearstack in aktprocdef.procoptions) then
  1446. begin
  1447. { complex return values are removed from stack in C code PM }
  1448. if paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1449. list.concat(Taicpu.Op_const(A_RET,S_NO,4))
  1450. else
  1451. list.concat(Taicpu.Op_none(A_RET,S_NO));
  1452. end
  1453. else if (parasize=0) then
  1454. list.concat(Taicpu.Op_none(A_RET,S_NO))
  1455. else
  1456. begin
  1457. { parameters are limited to 65535 bytes because }
  1458. { ret allows only imm16 }
  1459. if (parasize>65535) then
  1460. CGMessage(cg_e_parasize_too_big);
  1461. list.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  1462. end;
  1463. end;
  1464. {$ifndef TEST_GENERIC}
  1465. procedure tcgx86.g_call_constructor_helper(list : taasmoutput);
  1466. var r:Tregister;
  1467. begin
  1468. r.enum:=R_EDI;
  1469. if is_class(procinfo._class) then
  1470. begin
  1471. if (cs_implicit_exceptions in aktmoduleswitches) then
  1472. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1473. a_call_name(list,'FPC_NEW_CLASS');
  1474. list.concat(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1475. end
  1476. else if is_object(procinfo._class) then
  1477. begin
  1478. rg.getexplicitregisterint(list,R_EDI);
  1479. a_load_const_reg(list,OS_ADDR,procinfo._class.vmt_offset,r);
  1480. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1481. list.concat(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1482. end
  1483. else
  1484. internalerror(200006161);
  1485. end;
  1486. procedure tcgx86.g_call_destructor_helper(list : taasmoutput);
  1487. var
  1488. nofinal : tasmlabel;
  1489. href : treference;
  1490. r : Tregister;
  1491. begin
  1492. r.enum:=R_INTREGISTER;
  1493. if is_class(procinfo._class) then
  1494. begin
  1495. a_call_name(list,'FPC_DISPOSE_CLASS')
  1496. end
  1497. else if is_object(procinfo._class) then
  1498. begin
  1499. { must the object be finalized ? }
  1500. if procinfo._class.needs_inittable then
  1501. begin
  1502. objectlibrary.getlabel(nofinal);
  1503. r.number:=NR_EBP;
  1504. reference_reset_base(href,r,8);
  1505. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1506. r.number:=NR_ESI;
  1507. reference_reset_base(href,r,0);
  1508. g_finalize(list,procinfo._class,href,false);
  1509. a_label(list,nofinal);
  1510. end;
  1511. rg.getexplicitregisterint(list,R_EDI);
  1512. r.number:=NR_EDI;
  1513. a_load_const_reg(list,OS_ADDR,procinfo._class.vmt_offset,r);
  1514. r.enum:=R_EDI;
  1515. rg.ungetregisterint(list,r);
  1516. a_call_name(list,'FPC_HELP_DESTRUCTOR')
  1517. end
  1518. else
  1519. internalerror(200006162);
  1520. end;
  1521. procedure tcgx86.g_call_fail_helper(list : taasmoutput);
  1522. var
  1523. href : treference;
  1524. r : Tregister;
  1525. begin
  1526. r.enum:=R_INTREGISTER;
  1527. if is_class(procinfo._class) then
  1528. begin
  1529. reference_reset_base(href,procinfo.framepointer,8);
  1530. r.number:=NR_ESI;
  1531. a_load_ref_reg(list,OS_ADDR,href,r);
  1532. a_call_name(list,'FPC_HELP_FAIL_CLASS');
  1533. end
  1534. else if is_object(procinfo._class) then
  1535. begin
  1536. reference_reset_base(href,procinfo.framepointer,12);
  1537. r.number:=NR_ESI;
  1538. a_load_ref_reg(list,OS_ADDR,href,r);
  1539. rg.getexplicitregisterint(list,R_EDI);
  1540. r.number:=NR_EDI;
  1541. a_load_const_reg(list,OS_ADDR,procinfo._class.vmt_offset,r);
  1542. a_call_name(list,'FPC_HELP_FAIL');
  1543. r.enum:=R_EDI;
  1544. rg.ungetregisterint(list,r);
  1545. end
  1546. else
  1547. internalerror(200006163);
  1548. end;
  1549. {$endif}
  1550. procedure tcgx86.g_save_standard_registers(list : taasmoutput; usedinproc : tregisterset);
  1551. var r:Tregister;
  1552. begin
  1553. r.enum:=R_INTREGISTER;
  1554. r.number:=NR_EBX;
  1555. if (R_EBX in usedinproc) then
  1556. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1557. r.number:=NR_ESI;
  1558. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1559. r.number:=NR_EDI;
  1560. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1561. end;
  1562. procedure tcgx86.g_restore_standard_registers(list : taasmoutput; usedinproc : tregisterset);
  1563. var r:Tregister;
  1564. begin
  1565. r.enum:=R_INTREGISTER;
  1566. r.number:=NR_EDI;
  1567. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1568. r.number:=NR_ESI;
  1569. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1570. r.number:=NR_EBX;
  1571. if (R_EBX in usedinproc) then
  1572. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1573. end;
  1574. procedure tcgx86.g_save_all_registers(list : taasmoutput);
  1575. begin
  1576. list.concat(Taicpu.Op_none(A_PUSHA,S_L));
  1577. end;
  1578. procedure tcgx86.g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);
  1579. var
  1580. href : treference;
  1581. r,rsp: Tregister;
  1582. begin
  1583. rsp.enum:=R_INTREGISTER;
  1584. rsp.number:=NR_ESP;
  1585. r.enum:=R_INTREGISTER;
  1586. if selfused then
  1587. begin
  1588. reference_reset_base(href,rsp,4);
  1589. r.number:=NR_ESI;
  1590. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1591. end;
  1592. if acchiused then
  1593. begin
  1594. reference_reset_base(href,rsp,20);
  1595. r.number:=NR_EDX;
  1596. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1597. end;
  1598. if accused then
  1599. begin
  1600. reference_reset_base(href,rsp,28);
  1601. r.number:=NR_EAX;
  1602. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1603. end;
  1604. list.concat(Taicpu.Op_none(A_POPA,S_L));
  1605. { We add a NOP because of the 386DX CPU bugs with POPAD }
  1606. list.concat(taicpu.op_none(A_NOP,S_L));
  1607. end;
  1608. { produces if necessary overflowcode }
  1609. procedure tcgx86.g_overflowcheck(list: taasmoutput; const p: tnode);
  1610. var
  1611. hl : tasmlabel;
  1612. ai : taicpu;
  1613. cond : TAsmCond;
  1614. begin
  1615. if not(cs_check_overflow in aktlocalswitches) then
  1616. exit;
  1617. objectlibrary.getlabel(hl);
  1618. if not ((p.resulttype.def.deftype=pointerdef) or
  1619. ((p.resulttype.def.deftype=orddef) and
  1620. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1621. bool8bit,bool16bit,bool32bit]))) then
  1622. cond:=C_NO
  1623. else
  1624. cond:=C_NB;
  1625. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1626. ai.SetCondition(cond);
  1627. ai.is_jmp:=true;
  1628. list.concat(ai);
  1629. a_call_name(list,'FPC_OVERFLOW');
  1630. a_label(list,hl);
  1631. end;
  1632. end.
  1633. {
  1634. $Log$
  1635. Revision 1.28 2003-01-09 20:41:00 daniel
  1636. * Converted some code in cgx86.pas to new register numbering
  1637. Revision 1.27 2003/01/08 18:43:58 daniel
  1638. * Tregister changed into a record
  1639. Revision 1.26 2003/01/05 13:36:53 florian
  1640. * x86-64 compiles
  1641. + very basic support for float128 type (x86-64 only)
  1642. Revision 1.25 2003/01/02 16:17:50 peter
  1643. * align stack on 4 bytes in copyvalueopenarray
  1644. Revision 1.24 2002/12/24 15:56:50 peter
  1645. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1646. this for the pageprotection
  1647. Revision 1.23 2002/11/25 18:43:34 carl
  1648. - removed the invalid if <> checking (Delphi is strange on this)
  1649. + implemented abstract warning on instance creation of class with
  1650. abstract methods.
  1651. * some error message cleanups
  1652. Revision 1.22 2002/11/25 17:43:29 peter
  1653. * splitted defbase in defutil,symutil,defcmp
  1654. * merged isconvertable and is_equal into compare_defs(_ext)
  1655. * made operator search faster by walking the list only once
  1656. Revision 1.21 2002/11/18 17:32:01 peter
  1657. * pass proccalloption to ret_in_xxx and push_xxx functions
  1658. Revision 1.20 2002/11/09 21:18:31 carl
  1659. * flags2reg() was not extending the byte register to the correct result size
  1660. Revision 1.19 2002/10/16 19:01:43 peter
  1661. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1662. implicit exception frames for procedures with initialized variables
  1663. and for constructors. The default is on for compatibility
  1664. Revision 1.18 2002/10/05 12:43:30 carl
  1665. * fixes for Delphi 6 compilation
  1666. (warning : Some features do not work under Delphi)
  1667. Revision 1.17 2002/09/17 18:54:06 jonas
  1668. * a_load_reg_reg() now has two size parameters: source and dest. This
  1669. allows some optimizations on architectures that don't encode the
  1670. register size in the register name.
  1671. Revision 1.16 2002/09/16 19:08:47 peter
  1672. * support references without registers and symbol in paramref_addr. It
  1673. pushes only the offset
  1674. Revision 1.15 2002/09/16 18:06:29 peter
  1675. * move CGSize2Opsize to interface
  1676. Revision 1.14 2002/09/01 14:42:41 peter
  1677. * removevaluepara added to fix the stackpointer so restoring of
  1678. saved registers works
  1679. Revision 1.13 2002/09/01 12:09:27 peter
  1680. + a_call_reg, a_call_loc added
  1681. * removed exprasmlist references
  1682. Revision 1.12 2002/08/17 09:23:50 florian
  1683. * first part of procinfo rewrite
  1684. Revision 1.11 2002/08/16 14:25:00 carl
  1685. * issameref() to test if two references are the same (then emit no opcodes)
  1686. + ret_in_reg to replace ret_in_acc
  1687. (fix some register allocation bugs at the same time)
  1688. + save_std_register now has an extra parameter which is the
  1689. usedinproc registers
  1690. Revision 1.10 2002/08/15 08:13:54 carl
  1691. - a_load_sym_ofs_reg removed
  1692. * loadvmt now calls loadaddr_ref_reg instead
  1693. Revision 1.9 2002/08/11 14:32:33 peter
  1694. * renamed current_library to objectlibrary
  1695. Revision 1.8 2002/08/11 13:24:20 peter
  1696. * saving of asmsymbols in ppu supported
  1697. * asmsymbollist global is removed and moved into a new class
  1698. tasmlibrarydata that will hold the info of a .a file which
  1699. corresponds with a single module. Added librarydata to tmodule
  1700. to keep the library info stored for the module. In the future the
  1701. objectfiles will also be stored to the tasmlibrarydata class
  1702. * all getlabel/newasmsymbol and friends are moved to the new class
  1703. Revision 1.7 2002/08/10 10:06:04 jonas
  1704. * fixed stupid bug of mine in g_flags2reg() when optimizations are on
  1705. Revision 1.6 2002/08/09 19:18:27 carl
  1706. * fix generic exception handling
  1707. Revision 1.5 2002/08/04 19:52:04 carl
  1708. + updated exception routines
  1709. Revision 1.4 2002/07/27 19:53:51 jonas
  1710. + generic implementation of tcg.g_flags2ref()
  1711. * tcg.flags2xxx() now also needs a size parameter
  1712. Revision 1.3 2002/07/26 21:15:46 florian
  1713. * rewrote the system handling
  1714. Revision 1.2 2002/07/21 16:55:34 jonas
  1715. * fixed bug in op_const_reg_reg() for imul
  1716. Revision 1.1 2002/07/20 19:28:47 florian
  1717. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  1718. cgx86.pas will contain the common code for i386 and x86_64
  1719. }