cgx86.pas 70 KB

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