cgx86.pas 72 KB

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