cgx86.pas 74 KB

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