cgx86.pas 73 KB

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