cgx86.pas 75 KB

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