cgx86.pas 71 KB

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