cgobj.pas 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Member of the Free Pascal development team
  5. This unit implements the basic code generator object
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. {# @abstract(Abstract code generator unit)
  20. Abstreact code generator unit. This contains the base class
  21. to implement for all new supported processors.
  22. WARNING: None of the routines implemented in these modules,
  23. or their descendants, should use the temp. allocator, as
  24. these routines may be called inside genentrycode, and the
  25. stack frame is already setup!
  26. }
  27. unit cgobj;
  28. {$i fpcdefs.inc}
  29. interface
  30. uses
  31. {$ifdef delphi}
  32. dmisc,
  33. {$endif}
  34. cclasses,
  35. cpubase,cpuinfo,cgbase,
  36. aasmbase,aasmtai,aasmcpu,
  37. symconst,symbase,symtype,symdef,symtable,rgobj
  38. ;
  39. type
  40. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  41. {# @abstract(Abstract code generator)
  42. This class implements an abstract instruction generator. Some of
  43. the methods of this class are generic, while others must
  44. be overriden for all new processors which will be supported
  45. by Free Pascal. For 32-bit processors, the base class
  46. sould be @link(tcg64f32) and not @var(tcg).
  47. }
  48. tcg = class
  49. public
  50. alignment : talignment;
  51. rg : array[tregistertype] of trgobj;
  52. t_times:cardinal;
  53. {************************************************}
  54. { basic routines }
  55. constructor create;
  56. {# Initialize the register allocators needed for the codegenerator.}
  57. procedure init_register_allocators;virtual;
  58. {# Clean up the register allocators needed for the codegenerator.}
  59. procedure done_register_allocators;virtual;
  60. {# Gets a register suitable to do integer operations on.}
  61. function getintregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  62. {# Gets a register suitable to do integer operations on.}
  63. function getaddressregister(list:Taasmoutput):Tregister;virtual;
  64. function getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  65. function getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  66. function getflagregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;abstract;
  67. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  68. the cpu specific child cg object have such a method?}
  69. procedure ungetregister(list:Taasmoutput;r:Tregister);virtual;
  70. procedure ungetreference(list:Taasmoutput;const r:Treference);virtual;
  71. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  72. procedure add_move_instruction(instr:Taicpu);virtual;
  73. function uses_registers(rt:Tregistertype):boolean;virtual;
  74. {# Get a specific register.}
  75. procedure getexplicitregister(list:Taasmoutput;r:Tregister);virtual;
  76. {# Get multiple registers specified.}
  77. procedure allocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);virtual;
  78. {# Free multiple registers specified.}
  79. procedure deallocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);virtual;
  80. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;
  81. function makeregsize(reg:Tregister;size:Tcgsize):Tregister;
  82. {# Returns the tcgsize corresponding with the size of reg.}
  83. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  84. {# Emit a label to the instruction stream. }
  85. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  86. {# Allocates register r by inserting a pai_realloc record }
  87. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  88. {# Deallocates register r by inserting a pa_regdealloc record}
  89. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  90. {# Pass a parameter, which is located in a register, to a routine.
  91. This routine should push/send the parameter to the routine, as
  92. required by the specific processor ABI and routine modifiers.
  93. This must be overriden for each CPU target.
  94. @param(size size of the operand in the register)
  95. @param(r register source of the operand)
  96. @param(locpara where the parameter will be stored)
  97. }
  98. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);virtual;
  99. {# Pass a parameter, which is a constant, to a routine.
  100. A generic version is provided. This routine should
  101. be overriden for optimization purposes if the cpu
  102. permits directly sending this type of parameter.
  103. @param(size size of the operand in constant)
  104. @param(a value of constant to send)
  105. @param(locpara where the parameter will be stored)
  106. }
  107. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);virtual;
  108. {# Pass the value of a parameter, which is located in memory, to a routine.
  109. A generic version is provided. This routine should
  110. be overriden for optimization purposes if the cpu
  111. permits directly sending this type of parameter.
  112. @param(size size of the operand in constant)
  113. @param(r Memory reference of value to send)
  114. @param(locpara where the parameter will be stored)
  115. }
  116. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);virtual;
  117. {# Pass the value of a parameter, which can be located either in a register or memory location,
  118. to a routine.
  119. A generic version is provided.
  120. @param(l location of the operand to send)
  121. @param(nr parameter number (starting from one) of routine (from left to right))
  122. @param(locpara where the parameter will be stored)
  123. }
  124. procedure a_param_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);
  125. {# Pass the address of a reference to a routine. This routine
  126. will calculate the address of the reference, and pass this
  127. calculated address as a parameter.
  128. A generic version is provided. This routine should
  129. be overriden for optimization purposes if the cpu
  130. permits directly sending this type of parameter.
  131. @param(r reference to get address from)
  132. @param(nr parameter number (starting from one) of routine (from left to right))
  133. }
  134. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);virtual;
  135. { Copies a whole memory block to the stack, the locpara must be a memory location }
  136. procedure a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  137. { Remarks:
  138. * If a method specifies a size you have only to take care
  139. of that number of bits, i.e. load_const_reg with OP_8 must
  140. only load the lower 8 bit of the specified register
  141. the rest of the register can be undefined
  142. if necessary the compiler will call a method
  143. to zero or sign extend the register
  144. * The a_load_XX_XX with OP_64 needn't to be
  145. implemented for 32 bit
  146. processors, the code generator takes care of that
  147. * the addr size is for work with the natural pointer
  148. size
  149. * the procedures without fpu/mm are only for integer usage
  150. * normally the first location is the source and the
  151. second the destination
  152. }
  153. { Copy a parameter to a (temporary) reference }
  154. procedure a_loadany_param_ref(list : taasmoutput;const locpara : tparalocation;const ref:treference;shuffle : pmmshuffle);virtual;
  155. { Copy a parameter to a register }
  156. procedure a_loadany_param_reg(list : taasmoutput;const locpara : tparalocation;const reg:tregister;shuffle : pmmshuffle);virtual;
  157. {# Emits instruction to call the method specified by symbol name.
  158. This routine must be overriden for each new target cpu.
  159. There is no a_call_ref because loading the reference will use
  160. a temp register on most cpu's resulting in conflicts with the
  161. registers used for the parameters (PFV)
  162. }
  163. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  164. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual;abstract;
  165. { move instructions }
  166. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  167. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  168. procedure a_load_const_loc(list : taasmoutput;a : aword;const loc : tlocation);
  169. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  170. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  171. procedure a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  172. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  173. procedure a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  174. procedure a_load_loc_reg(list : taasmoutput;tosize: tcgsize; const loc: tlocation; reg : tregister);
  175. procedure a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  176. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  177. { fpu move instructions }
  178. procedure a_loadfpu_reg_reg(list: taasmoutput; size:tcgsize; reg1, reg2: tregister); virtual; abstract;
  179. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  180. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  181. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  182. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  183. procedure a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);virtual;
  184. procedure a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const locpara : tparalocation);virtual;
  185. { vector register move instructions }
  186. procedure a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual; abstract;
  187. procedure a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual; abstract;
  188. procedure a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual; abstract;
  189. procedure a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  190. procedure a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  191. procedure a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const locpara : tparalocation;shuffle : pmmshuffle); virtual;
  192. procedure a_parammm_ref(list: taasmoutput; size: tcgsize; const ref: treference;const locpara : tparalocation;shuffle : pmmshuffle); virtual;
  193. procedure a_parammm_loc(list: taasmoutput; const loc: tlocation; const locpara : tparalocation;shuffle : pmmshuffle); virtual;
  194. procedure a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;abstract;
  195. procedure a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  196. procedure a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  197. procedure a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  198. { basic arithmetic operations }
  199. { note: for operators which require only one argument (not, neg), use }
  200. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  201. { that in this case the *second* operand is used as both source and }
  202. { destination (JM) }
  203. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); virtual; abstract;
  204. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  205. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  206. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  207. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  208. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  209. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  210. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  211. { trinary operations for processors that support them, 'emulated' }
  212. { on others. None with "ref" arguments since I don't think there }
  213. { are any processors that support it (JM) }
  214. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  215. size: tcgsize; a: aword; src, dst: tregister); virtual;
  216. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  217. size: tcgsize; src1, src2, dst: tregister); virtual;
  218. { comparison operations }
  219. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  220. l : tasmlabel);virtual; abstract;
  221. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  222. l : tasmlabel); virtual;
  223. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  224. l : tasmlabel);
  225. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  226. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  227. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  228. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  229. l : tasmlabel);
  230. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  231. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  232. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  233. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  234. }
  235. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  236. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  237. {
  238. This routine tries to optimize the const_reg opcode, and should be
  239. called at the start of a_op_const_reg. It returns the actual opcode
  240. to emit, and the constant value to emit. If this routine returns
  241. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  242. @param(op The opcode to emit, returns the opcode which must be emitted)
  243. @param(a The constant which should be emitted, returns the constant which must
  244. be emitted)
  245. @param(reg The register to emit the opcode with, returns the register with
  246. which the opcode will be emitted)
  247. }
  248. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg: tregister): boolean;virtual;
  249. {#
  250. This routine is used in exception management nodes. It should
  251. save the exception reason currently in the FUNCTION_RETURN_REG. The
  252. save should be done either to a temp (pointed to by href).
  253. or on the stack (pushing the value on the stack).
  254. The size of the value to save is OS_S32. The default version
  255. saves the exception reason to a temp. memory area.
  256. }
  257. procedure g_exception_reason_save(list : taasmoutput; const href : treference);virtual;
  258. {#
  259. This routine is used in exception management nodes. It should
  260. save the exception reason constant. The
  261. save should be done either to a temp (pointed to by href).
  262. or on the stack (pushing the value on the stack).
  263. The size of the value to save is OS_S32. The default version
  264. saves the exception reason to a temp. memory area.
  265. }
  266. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);virtual;
  267. {#
  268. This routine is used in exception management nodes. It should
  269. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  270. should either be in the temp. area (pointed to by href , href should
  271. *NOT* be freed) or on the stack (the value should be popped).
  272. The size of the value to save is OS_S32. The default version
  273. saves the exception reason to a temp. memory area.
  274. }
  275. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  276. procedure g_maybe_testself(list : taasmoutput;reg:tregister);
  277. procedure g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  278. {# This should emit the opcode to copy len bytes from the source
  279. to destination, if loadref is true, it assumes that it first must load
  280. the source address from the memory location where
  281. source points to.
  282. It must be overriden for each new target processor.
  283. @param(source Source reference of copy)
  284. @param(dest Destination reference of copy)
  285. @param(delsource Indicates if the source reference's resources should be freed)
  286. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  287. }
  288. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  289. {# This should emit the opcode to a shortrstring from the source
  290. to destination, if loadref is true, it assumes that it first must load
  291. the source address from the memory location where
  292. source points to.
  293. @param(source Source reference of copy)
  294. @param(dest Destination reference of copy)
  295. @param(delsource Indicates if the source reference's resources should be freed)
  296. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  297. }
  298. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  299. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  300. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  301. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  302. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  303. {# Emits the call to the stack checking routine of
  304. the runtime library. The default behavior
  305. does not need to be modified, as it is generic
  306. for all platforms.
  307. @param(stackframesize Number of bytes which will be allocated on the stack)
  308. }
  309. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  310. {# Generates range checking code. It is to note
  311. that this routine does not need to be overriden,
  312. as it takes care of everything.
  313. @param(p Node which contains the value to check)
  314. @param(todef Type definition of node to range check)
  315. }
  316. procedure g_rangecheck(list: taasmoutput; const l:tlocation; fromdef,todef: tdef); virtual;
  317. {# Generates overflow checking code for a node }
  318. procedure g_overflowcheck(list: taasmoutput; const l:tlocation; def:tdef); virtual; abstract;
  319. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:aword);virtual;abstract;
  320. procedure g_releasevaluepara_openarray(list : taasmoutput;const ref:treference);virtual;
  321. {# Emits instructions which should be emitted when entering
  322. a routine declared as @var(interrupt). The default
  323. behavior does nothing, should be overriden as required.
  324. }
  325. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  326. {# Emits instructions which should be emitted when exiting
  327. a routine declared as @var(interrupt). The default
  328. behavior does nothing, should be overriden as required.
  329. }
  330. procedure g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);virtual;
  331. {# Emits instructions when compilation is done in profile
  332. mode (this is set as a command line option). The default
  333. behavior does nothing, should be overriden as required.
  334. }
  335. procedure g_profilecode(list : taasmoutput);virtual;
  336. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  337. @param(size Number of bytes to allocate)
  338. }
  339. procedure g_stackpointer_alloc(list : taasmoutput;size : longint);virtual; abstract;
  340. {# Emits instruction for allocating the locals in entry
  341. code of a routine. This is one of the first
  342. routine called in @var(genentrycode).
  343. @param(localsize Number of bytes to allocate as locals)
  344. }
  345. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  346. {# Emits instructiona for restoring the frame pointer
  347. at routine exit. For some processors, this routine
  348. may do nothing at all.
  349. }
  350. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  351. {# Emits instructions for returning from a subroutine.
  352. Should also restore the stack.
  353. @param(parasize Number of bytes of parameters to deallocate from stack)
  354. }
  355. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  356. {# This routine is called when generating the code for the entry point
  357. of a routine. It should save all registers which are not used in this
  358. routine, and which should be declared as saved in the std_saved_registers
  359. set.
  360. This routine is mainly used when linking to code which is generated
  361. by ABI-compliant compilers (like GCC), to make sure that the reserved
  362. registers of that ABI are not clobbered.
  363. @param(usedinproc Registers which are used in the code of this routine)
  364. }
  365. procedure g_save_standard_registers(list:Taasmoutput);virtual;abstract;
  366. {# This routine is called when generating the code for the exit point
  367. of a routine. It should restore all registers which were previously
  368. saved in @var(g_save_standard_registers).
  369. @param(usedinproc Registers which are used in the code of this routine)
  370. }
  371. procedure g_restore_standard_registers(list:Taasmoutput);virtual;abstract;
  372. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  373. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);virtual;abstract;
  374. end;
  375. {# @abstract(Abstract code generator for 64 Bit operations)
  376. This class implements an abstract code generator class
  377. for 64 Bit operations.
  378. }
  379. tcg64 = class
  380. { Allocates 64 Bit register r by inserting a pai_realloc record }
  381. procedure a_reg_alloc(list : taasmoutput;r : tregister64);virtual;abstract;
  382. { Deallocates 64 Bit register r by inserting a pa_regdealloc record}
  383. procedure a_reg_dealloc(list : taasmoutput;r : tregister64);virtual;abstract;
  384. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);virtual;abstract;
  385. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  386. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  387. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  388. procedure a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);virtual;abstract;
  389. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  390. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  391. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);virtual;abstract;
  392. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  393. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  394. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  395. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  396. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  397. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  398. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  399. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  400. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  401. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  402. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;regdst : tregister64);virtual;abstract;
  403. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);virtual;abstract;
  404. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);virtual;abstract;
  405. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  406. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  407. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);virtual;
  408. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  409. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  410. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  411. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  412. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  413. {
  414. This routine tries to optimize the const_reg opcode, and should be
  415. called at the start of a_op64_const_reg. It returns the actual opcode
  416. to emit, and the constant value to emit. If this routine returns
  417. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  418. @param(op The opcode to emit, returns the opcode which must be emitted)
  419. @param(a The constant which should be emitted, returns the constant which must
  420. be emitted)
  421. @param(reg The register to emit the opcode with, returns the register with
  422. which the opcode will be emitted)
  423. }
  424. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;virtual;abstract;
  425. { override to catch 64bit rangechecks }
  426. procedure g_rangecheck64(list: taasmoutput; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  427. end;
  428. { trerefence handling }
  429. {# Clear to zero a treference }
  430. procedure reference_reset(var ref : treference);
  431. {# Clear to zero a treference, and set is base address
  432. to base register.
  433. }
  434. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  435. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  436. procedure reference_release(list: taasmoutput; const ref : treference);
  437. { This routine verifies if two references are the same, and
  438. if so, returns TRUE, otherwise returns false.
  439. }
  440. function references_equal(sref : treference;dref : treference) : boolean;
  441. { tlocation handling }
  442. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  443. procedure location_release(list: taasmoutput; const l : tlocation);
  444. procedure location_freetemp(list: taasmoutput; const l : tlocation);
  445. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  446. procedure location_swap(var destloc,sourceloc : tlocation);
  447. var
  448. {# Main code generator class }
  449. cg : tcg;
  450. {# Code generator class for all operations working with 64-Bit operands }
  451. cg64 : tcg64;
  452. implementation
  453. uses
  454. globals,globtype,options,systems,
  455. verbose,defutil,paramgr,
  456. tgobj,cutils;
  457. const
  458. { Please leave this here, this module should NOT use
  459. exprasmlist, the lists are always passed as arguments.
  460. Declaring it as string here results in an error when compiling (PFV) }
  461. exprasmlist = 'error';
  462. {*****************************************************************************
  463. basic functionallity
  464. ******************************************************************************}
  465. constructor tcg.create;
  466. begin
  467. end;
  468. function Tcg.makeregsize(reg:Tregister;size:Tcgsize):Tregister;
  469. var
  470. subreg:Tsubregister;
  471. begin
  472. subreg:=cgsize2subreg(size);
  473. result:=reg;
  474. setsubreg(result,subreg);
  475. end;
  476. {*****************************************************************************
  477. register allocation
  478. ******************************************************************************}
  479. procedure tcg.init_register_allocators;
  480. begin
  481. fillchar(rg,sizeof(rg),0);
  482. add_reg_instruction_hook:={$ifdef FPCPROCVAR}@{$endif}add_reg_instruction;
  483. end;
  484. procedure tcg.done_register_allocators;
  485. begin
  486. { Safety }
  487. fillchar(rg,sizeof(rg),0);
  488. add_reg_instruction_hook:=nil;
  489. end;
  490. function tcg.getintregister(list:Taasmoutput;size:Tcgsize):Tregister;
  491. begin
  492. if not assigned(rg[R_INTREGISTER]) then
  493. internalerror(200312122);
  494. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(size));
  495. end;
  496. function tcg.getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;
  497. begin
  498. if not assigned(rg[R_FPUREGISTER]) then
  499. internalerror(200312123);
  500. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(size));
  501. end;
  502. function tcg.getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;
  503. begin
  504. if not assigned(rg[R_MMREGISTER]) then
  505. internalerror(200312124);
  506. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(size));
  507. end;
  508. function tcg.getaddressregister(list:Taasmoutput):Tregister;
  509. begin
  510. if assigned(rg[R_ADDRESSREGISTER]) then
  511. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  512. else
  513. begin
  514. if not assigned(rg[R_INTREGISTER]) then
  515. internalerror(200312121);
  516. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  517. end;
  518. end;
  519. procedure tcg.getexplicitregister(list:Taasmoutput;r:Tregister);
  520. begin
  521. if not assigned(rg[getregtype(r)]) then
  522. internalerror(200312125);
  523. rg[getregtype(r)].getexplicitregister(list,r);
  524. end;
  525. procedure tcg.ungetregister(list:Taasmoutput;r:Tregister);
  526. begin
  527. if not assigned(rg[getregtype(r)]) then
  528. internalerror(200312126);
  529. rg[getregtype(r)].ungetregister(list,r);
  530. end;
  531. procedure tcg.ungetreference(list:Taasmoutput;const r:Treference);
  532. begin
  533. if r.base<>NR_NO then
  534. ungetregister(list,r.base);
  535. if r.index<>NR_NO then
  536. ungetregister(list,r.index);
  537. end;
  538. procedure tcg.allocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  539. begin
  540. if assigned(rg[rt]) then
  541. rg[rt].allocexplicitregisters(list,r)
  542. else
  543. internalerror(200310092);
  544. end;
  545. procedure tcg.deallocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  546. begin
  547. if assigned(rg[rt]) then
  548. rg[rt].deallocexplicitregisters(list,r)
  549. else
  550. internalerror(200310093);
  551. end;
  552. function tcg.uses_registers(rt:Tregistertype):boolean;
  553. begin
  554. if assigned(rg[rt]) then
  555. result:=rg[rt].uses_registers
  556. else
  557. result:=false;
  558. end;
  559. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  560. var
  561. rt : tregistertype;
  562. begin
  563. rt:=getregtype(r);
  564. { Only add it when a register allocator is configured.
  565. No IE can be generated, because the VMT is written
  566. without a valid rg[] }
  567. if assigned(rg[rt]) then
  568. rg[rt].add_reg_instruction(instr,r);
  569. end;
  570. procedure tcg.add_move_instruction(instr:Taicpu);
  571. var
  572. rt : tregistertype;
  573. begin
  574. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  575. if assigned(rg[rt]) then
  576. rg[rt].add_move_instruction(instr)
  577. else
  578. internalerror(200310095);
  579. end;
  580. procedure tcg.do_register_allocation(list:Taasmoutput;headertai:tai);
  581. var
  582. rt : tregistertype;
  583. begin
  584. for rt:=low(tregistertype) to high(tregistertype) do
  585. begin
  586. if assigned(rg[rt]) then
  587. rg[rt].do_register_allocation(list,headertai);
  588. end;
  589. end;
  590. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  591. begin
  592. list.concat(tai_regalloc.alloc(r));
  593. end;
  594. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  595. begin
  596. list.concat(tai_regalloc.dealloc(r));
  597. end;
  598. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  599. begin
  600. list.concat(tai_label.create(l));
  601. end;
  602. {*****************************************************************************
  603. for better code generation these methods should be overridden
  604. ******************************************************************************}
  605. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  606. var
  607. ref : treference;
  608. begin
  609. case locpara.loc of
  610. LOC_REGISTER,LOC_CREGISTER:
  611. a_load_reg_reg(list,size,locpara.size,r,locpara.register);
  612. LOC_REFERENCE,LOC_CREFERENCE:
  613. begin
  614. reference_reset(ref);
  615. ref.base:=locpara.reference.index;
  616. ref.offset:=locpara.reference.offset;
  617. a_load_reg_ref(list,size,locpara.size,r,ref);
  618. end
  619. else
  620. internalerror(2002071004);
  621. end;
  622. end;
  623. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  624. var
  625. ref : treference;
  626. begin
  627. case locpara.loc of
  628. LOC_REGISTER,LOC_CREGISTER:
  629. a_load_const_reg(list,locpara.size,a,locpara.register);
  630. LOC_REFERENCE,LOC_CREFERENCE:
  631. begin
  632. reference_reset(ref);
  633. ref.base:=locpara.reference.index;
  634. ref.offset:=locpara.reference.offset;
  635. a_load_const_ref(list,locpara.size,a,ref);
  636. end
  637. else
  638. internalerror(2002071004);
  639. end;
  640. end;
  641. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  642. var
  643. ref : treference;
  644. begin
  645. case locpara.loc of
  646. LOC_REGISTER,LOC_CREGISTER:
  647. a_load_ref_reg(list,size,locpara.size,r,locpara.register);
  648. LOC_REFERENCE,LOC_CREFERENCE:
  649. begin
  650. reference_reset(ref);
  651. ref.base:=locpara.reference.index;
  652. ref.offset:=locpara.reference.offset;
  653. a_load_ref_ref(list,size,locpara.size,r,ref);
  654. end
  655. else
  656. internalerror(2002071004);
  657. end;
  658. end;
  659. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  660. begin
  661. case l.loc of
  662. LOC_REGISTER,
  663. LOC_CREGISTER :
  664. a_param_reg(list,l.size,l.register,locpara);
  665. LOC_CONSTANT :
  666. a_param_const(list,l.size,l.value,locpara);
  667. LOC_CREFERENCE,
  668. LOC_REFERENCE :
  669. a_param_ref(list,l.size,l.reference,locpara);
  670. else
  671. internalerror(2002032211);
  672. end;
  673. end;
  674. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  675. var
  676. hr : tregister;
  677. begin
  678. hr:=getaddressregister(list);
  679. a_loadaddr_ref_reg(list,r,hr);
  680. ungetregister(list,hr);
  681. a_param_reg(list,OS_ADDR,hr,locpara);
  682. end;
  683. procedure tcg.a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  684. var
  685. ref : treference;
  686. begin
  687. if not(locpara.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  688. internalerror(2003010901);
  689. reference_reset_base(ref,locpara.reference.index,locpara.reference.offset);
  690. g_concatcopy(list,r,ref,size,false,false);
  691. end;
  692. procedure tcg.a_loadany_param_ref(list : taasmoutput;const locpara : tparalocation;const ref:treference;shuffle : pmmshuffle);
  693. begin
  694. case locpara.loc of
  695. LOC_CREGISTER,
  696. LOC_REGISTER:
  697. begin
  698. if (locpara.size in [OS_S64,OS_64]) then
  699. begin
  700. {$ifdef cpu64bit}
  701. ungetregister(list,locpara.register64);
  702. {$else cpu64bit}
  703. getexplicitregister(list,locpara.registerlow);
  704. getexplicitregister(list,locpara.registerhigh);
  705. ungetregister(list,locpara.registerlow);
  706. ungetregister(list,locpara.registerhigh);
  707. {$endif cpu64bit}
  708. cg64.a_load64_reg_ref(list,locpara.register64,ref)
  709. end
  710. else
  711. begin
  712. getexplicitregister(list,locpara.register);
  713. ungetregister(list,locpara.register);
  714. a_load_reg_ref(list,locpara.size,locpara.size,locpara.register,ref);
  715. end;
  716. end;
  717. LOC_MMREGISTER,
  718. LOC_CMMREGISTER:
  719. begin
  720. getexplicitregister(list,locpara.register);
  721. ungetregister(list,locpara.register);
  722. a_loadmm_reg_ref(list,locpara.size,locpara.size,locpara.register,ref,shuffle);
  723. end;
  724. LOC_FPUREGISTER,
  725. LOC_CFPUREGISTER:
  726. begin
  727. getexplicitregister(list,locpara.register);
  728. ungetregister(list,locpara.register);
  729. a_loadfpu_reg_ref(list,locpara.size,locpara.register,ref);
  730. end;
  731. else
  732. internalerror(2002081302);
  733. end;
  734. end;
  735. procedure tcg.a_loadany_param_reg(list : taasmoutput;const locpara : tparalocation;const reg:tregister;shuffle : pmmshuffle);
  736. var
  737. href : treference;
  738. begin
  739. case locpara.loc of
  740. LOC_CREGISTER,
  741. LOC_REGISTER:
  742. begin
  743. if not(locpara.size in [OS_S64,OS_64]) then
  744. begin
  745. getexplicitregister(list,locpara.register);
  746. ungetregister(list,locpara.register);
  747. getexplicitregister(list,reg);
  748. a_load_reg_reg(list,locpara.size,locpara.size,locpara.register,reg)
  749. end
  750. else
  751. internalerror(2003053011);
  752. end;
  753. LOC_CFPUREGISTER,
  754. LOC_FPUREGISTER:
  755. begin
  756. getexplicitregister(list,locpara.register);
  757. ungetregister(list,locpara.register);
  758. getexplicitregister(list,reg);
  759. a_loadfpu_reg_reg(list,locpara.size,locpara.register,reg);
  760. end;
  761. LOC_MMREGISTER,
  762. LOC_CMMREGISTER:
  763. begin
  764. getexplicitregister(list,locpara.register);
  765. ungetregister(list,locpara.register);
  766. getexplicitregister(list,reg);
  767. a_loadmm_reg_reg(list,locpara.size,locpara.size,locpara.register,reg,shuffle);
  768. end;
  769. LOC_REFERENCE,
  770. LOC_CREFERENCE:
  771. begin
  772. reference_reset_base(href,locpara.reference.index,locpara.reference.offset);
  773. getexplicitregister(list,reg);
  774. a_load_ref_reg(list,locpara.size,locpara.size,href,reg);
  775. end;
  776. else
  777. internalerror(2003053010);
  778. end
  779. end;
  780. {****************************************************************************
  781. some generic implementations
  782. ****************************************************************************}
  783. procedure tcg.a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  784. var
  785. tmpreg: tregister;
  786. begin
  787. { verify if we have the same reference }
  788. if references_equal(sref,dref) then
  789. exit;
  790. tmpreg:=getintregister(list,tosize);
  791. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  792. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  793. ungetregister(list,tmpreg);
  794. end;
  795. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  796. var
  797. tmpreg: tregister;
  798. begin
  799. tmpreg:=getintregister(list,size);
  800. a_load_const_reg(list,size,a,tmpreg);
  801. a_load_reg_ref(list,size,size,tmpreg,ref);
  802. ungetregister(list,tmpreg);
  803. end;
  804. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  805. begin
  806. case loc.loc of
  807. LOC_REFERENCE,LOC_CREFERENCE:
  808. a_load_const_ref(list,loc.size,a,loc.reference);
  809. LOC_REGISTER,LOC_CREGISTER:
  810. a_load_const_reg(list,loc.size,a,loc.register);
  811. else
  812. internalerror(200203272);
  813. end;
  814. end;
  815. procedure tcg.a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  816. begin
  817. case loc.loc of
  818. LOC_REFERENCE,LOC_CREFERENCE:
  819. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  820. LOC_REGISTER,LOC_CREGISTER:
  821. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  822. else
  823. internalerror(200203271);
  824. end;
  825. end;
  826. procedure tcg.a_load_loc_reg(list : taasmoutput; tosize: tcgsize; const loc: tlocation; reg : tregister);
  827. begin
  828. case loc.loc of
  829. LOC_REFERENCE,LOC_CREFERENCE:
  830. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  831. LOC_REGISTER,LOC_CREGISTER:
  832. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  833. LOC_CONSTANT:
  834. a_load_const_reg(list,tosize,loc.value,reg);
  835. else
  836. internalerror(200109092);
  837. end;
  838. end;
  839. procedure tcg.a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  840. begin
  841. case loc.loc of
  842. LOC_REFERENCE,LOC_CREFERENCE:
  843. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  844. LOC_REGISTER,LOC_CREGISTER:
  845. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  846. LOC_CONSTANT:
  847. a_load_const_ref(list,tosize,loc.value,ref);
  848. else
  849. internalerror(200109302);
  850. end;
  851. end;
  852. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg:tregister): boolean;
  853. var
  854. powerval : longint;
  855. begin
  856. optimize_op_const_reg := false;
  857. case op of
  858. { or with zero returns same result }
  859. OP_OR : if a = 0 then optimize_op_const_reg := true;
  860. { and with max returns same result }
  861. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  862. { division by 1 returns result }
  863. OP_DIV :
  864. begin
  865. if a = 1 then
  866. optimize_op_const_reg := true
  867. else if ispowerof2(int64(a), powerval) then
  868. begin
  869. a := powerval;
  870. op:= OP_SHR;
  871. end;
  872. exit;
  873. end;
  874. OP_IDIV:
  875. begin
  876. if a = 1 then
  877. optimize_op_const_reg := true
  878. else if ispowerof2(int64(a), powerval) then
  879. begin
  880. a := powerval;
  881. op:= OP_SAR;
  882. end;
  883. exit;
  884. end;
  885. OP_MUL,OP_IMUL:
  886. begin
  887. if a = 1 then
  888. optimize_op_const_reg := true
  889. else if ispowerof2(int64(a), powerval) then
  890. begin
  891. a := powerval;
  892. op:= OP_SHL;
  893. end;
  894. exit;
  895. end;
  896. OP_SAR,OP_SHL,OP_SHR:
  897. begin
  898. if a = 0 then
  899. optimize_op_const_reg := true;
  900. exit;
  901. end;
  902. end;
  903. end;
  904. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  905. begin
  906. case loc.loc of
  907. LOC_REFERENCE, LOC_CREFERENCE:
  908. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  909. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  910. a_loadfpu_reg_reg(list,loc.size,loc.register,reg);
  911. else
  912. internalerror(200203301);
  913. end;
  914. end;
  915. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  916. begin
  917. case loc.loc of
  918. LOC_REFERENCE, LOC_CREFERENCE:
  919. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  920. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  921. a_loadfpu_reg_reg(list,size,reg,loc.register);
  922. else
  923. internalerror(48991);
  924. end;
  925. end;
  926. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);
  927. var
  928. ref : treference;
  929. begin
  930. case locpara.loc of
  931. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  932. a_loadfpu_reg_reg(list,size,r,locpara.register);
  933. LOC_REFERENCE,LOC_CREFERENCE:
  934. begin
  935. reference_reset(ref);
  936. ref.base:=locpara.reference.index;
  937. ref.offset:=locpara.reference.offset;
  938. a_loadfpu_reg_ref(list,size,r,ref);
  939. end
  940. else
  941. internalerror(2002071004);
  942. end;
  943. end;
  944. procedure tcg.a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const locpara : tparalocation);
  945. var
  946. hr : tregister;
  947. begin
  948. hr:=getfpuregister(list,size);
  949. a_loadfpu_ref_reg(list,size,ref,hr);
  950. a_paramfpu_reg(list,size,hr,locpara);
  951. ungetregister(list,hr);
  952. end;
  953. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  954. var
  955. tmpreg: tregister;
  956. begin
  957. tmpreg:=getintregister(list,size);
  958. a_load_ref_reg(list,size,size,ref,tmpreg);
  959. a_op_const_reg(list,op,size,a,tmpreg);
  960. a_load_reg_ref(list,size,size,tmpreg,ref);
  961. ungetregister(list,tmpreg);
  962. end;
  963. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  964. begin
  965. case loc.loc of
  966. LOC_REGISTER, LOC_CREGISTER:
  967. a_op_const_reg(list,op,loc.size,a,loc.register);
  968. LOC_REFERENCE, LOC_CREFERENCE:
  969. a_op_const_ref(list,op,loc.size,a,loc.reference);
  970. else
  971. internalerror(200109061);
  972. end;
  973. end;
  974. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  975. var
  976. tmpreg: tregister;
  977. begin
  978. tmpreg:=getintregister(list,size);
  979. a_load_ref_reg(list,size,size,ref,tmpreg);
  980. a_op_reg_reg(list,op,size,reg,tmpreg);
  981. a_load_reg_ref(list,size,size,tmpreg,ref);
  982. ungetregister(list,tmpreg);
  983. end;
  984. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  985. var
  986. tmpreg: tregister;
  987. begin
  988. case op of
  989. OP_NOT,OP_NEG:
  990. { handle it as "load ref,reg; op reg" }
  991. begin
  992. a_load_ref_reg(list,size,size,ref,reg);
  993. a_op_reg_reg(list,op,size,reg,reg);
  994. end;
  995. else
  996. begin
  997. tmpreg:=getintregister(list,size);
  998. a_load_ref_reg(list,size,size,ref,tmpreg);
  999. a_op_reg_reg(list,op,size,tmpreg,reg);
  1000. ungetregister(list,tmpreg);
  1001. end;
  1002. end;
  1003. end;
  1004. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  1005. begin
  1006. case loc.loc of
  1007. LOC_REGISTER, LOC_CREGISTER:
  1008. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1009. LOC_REFERENCE, LOC_CREFERENCE:
  1010. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1011. else
  1012. internalerror(200109061);
  1013. end;
  1014. end;
  1015. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1016. var
  1017. tmpreg: tregister;
  1018. begin
  1019. case loc.loc of
  1020. LOC_REGISTER,LOC_CREGISTER:
  1021. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1022. LOC_REFERENCE,LOC_CREFERENCE:
  1023. begin
  1024. tmpreg:=getintregister(list,loc.size);
  1025. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1026. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1027. ungetregister(list,tmpreg);
  1028. end;
  1029. else
  1030. internalerror(200109061);
  1031. end;
  1032. end;
  1033. procedure Tcg.a_op_const_reg_reg(list:Taasmoutput;op:Topcg;size:Tcgsize;
  1034. a:aword;src,dst:Tregister);
  1035. begin
  1036. a_load_reg_reg(list,size,size,src,dst);
  1037. a_op_const_reg(list,op,size,a,dst);
  1038. end;
  1039. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  1040. size: tcgsize; src1, src2, dst: tregister);
  1041. var
  1042. tmpreg: tregister;
  1043. begin
  1044. if (dst<>src1) then
  1045. begin
  1046. a_load_reg_reg(list,size,size,src2,dst);
  1047. a_op_reg_reg(list,op,size,src1,dst);
  1048. end
  1049. else
  1050. begin
  1051. tmpreg:=cg.getintregister(list,size);
  1052. a_load_reg_reg(list,size,size,src2,tmpreg);
  1053. a_op_reg_reg(list,op,size,src1,tmpreg);
  1054. a_load_reg_reg(list,size,size,tmpreg,dst);
  1055. ungetregister(list,tmpreg);
  1056. end;
  1057. end;
  1058. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  1059. l : tasmlabel);
  1060. var
  1061. tmpreg: tregister;
  1062. begin
  1063. tmpreg:=getintregister(list,size);
  1064. a_load_ref_reg(list,size,size,ref,tmpreg);
  1065. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1066. ungetregister(list,tmpreg);
  1067. end;
  1068. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  1069. l : tasmlabel);
  1070. begin
  1071. case loc.loc of
  1072. LOC_REGISTER,LOC_CREGISTER:
  1073. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1074. LOC_REFERENCE,LOC_CREFERENCE:
  1075. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1076. else
  1077. internalerror(200109061);
  1078. end;
  1079. end;
  1080. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1081. var
  1082. tmpreg: tregister;
  1083. begin
  1084. tmpreg:=getintregister(list,size);
  1085. a_load_ref_reg(list,size,size,ref,tmpreg);
  1086. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1087. ungetregister(list,tmpreg);
  1088. end;
  1089. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1090. begin
  1091. case loc.loc of
  1092. LOC_REGISTER,
  1093. LOC_CREGISTER:
  1094. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1095. LOC_REFERENCE,
  1096. LOC_CREFERENCE :
  1097. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1098. LOC_CONSTANT:
  1099. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1100. else
  1101. internalerror(200203231);
  1102. end;
  1103. end;
  1104. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1105. l : tasmlabel);
  1106. var
  1107. tmpreg: tregister;
  1108. begin
  1109. case loc.loc of
  1110. LOC_REGISTER,LOC_CREGISTER:
  1111. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1112. LOC_REFERENCE,LOC_CREFERENCE:
  1113. begin
  1114. tmpreg:=getintregister(list,size);
  1115. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1116. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1117. ungetregister(list,tmpreg);
  1118. end
  1119. else
  1120. internalerror(200109061);
  1121. end;
  1122. end;
  1123. procedure tcg.a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1124. begin
  1125. case loc.loc of
  1126. LOC_MMREGISTER,LOC_CMMREGISTER:
  1127. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1128. LOC_REFERENCE,LOC_CREFERENCE:
  1129. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1130. else
  1131. internalerror(200310121);
  1132. end;
  1133. end;
  1134. procedure tcg.a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1135. var
  1136. tmpreg: tregister;
  1137. begin
  1138. case loc.loc of
  1139. LOC_MMREGISTER,LOC_CMMREGISTER:
  1140. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1141. LOC_REFERENCE,LOC_CREFERENCE:
  1142. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1143. else
  1144. internalerror(200310122);
  1145. end;
  1146. end;
  1147. procedure tcg.a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const locpara : tparalocation;shuffle : pmmshuffle);
  1148. var
  1149. ref : treference;
  1150. begin
  1151. case locpara.loc of
  1152. LOC_MMREGISTER,LOC_CMMREGISTER:
  1153. a_loadmm_reg_reg(list,size,locpara.size,reg,locpara.register,shuffle);
  1154. LOC_REFERENCE,LOC_CREFERENCE:
  1155. begin
  1156. reference_reset(ref);
  1157. ref.base:=locpara.reference.index;
  1158. ref.offset:=locpara.reference.offset;
  1159. a_loadmm_reg_ref(list,size,locpara.size,reg,ref,shuffle);
  1160. end
  1161. else
  1162. internalerror(200310123);
  1163. end;
  1164. end;
  1165. procedure tcg.a_parammm_ref(list: taasmoutput; size: tcgsize;const ref: treference;const locpara : tparalocation;shuffle : pmmshuffle);
  1166. var
  1167. hr : tregister;
  1168. hs : tmmshuffle;
  1169. begin
  1170. hr:=getmmregister(list,locpara.size);
  1171. a_loadmm_ref_reg(list,size,locpara.size,ref,hr,shuffle);
  1172. if realshuffle(shuffle) then
  1173. begin
  1174. hs:=shuffle^;
  1175. removeshuffles(hs);
  1176. a_parammm_reg(list,locpara.size,hr,locpara,@hs);
  1177. end
  1178. else
  1179. a_parammm_reg(list,locpara.size,hr,locpara,shuffle);
  1180. ungetregister(list,hr);
  1181. end;
  1182. procedure tcg.a_parammm_loc(list: taasmoutput;const loc: tlocation; const locpara : tparalocation;shuffle : pmmshuffle);
  1183. begin
  1184. case loc.loc of
  1185. LOC_MMREGISTER,LOC_CMMREGISTER:
  1186. a_parammm_reg(list,loc.size,loc.register,locpara,shuffle);
  1187. LOC_REFERENCE,LOC_CREFERENCE:
  1188. a_parammm_ref(list,loc.size,loc.reference,locpara,shuffle);
  1189. else
  1190. internalerror(200310123);
  1191. end;
  1192. end;
  1193. procedure tcg.a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1194. var
  1195. hr : tregister;
  1196. hs : tmmshuffle;
  1197. begin
  1198. hr:=getmmregister(list,size);
  1199. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1200. if realshuffle(shuffle) then
  1201. begin
  1202. hs:=shuffle^;
  1203. removeshuffles(hs);
  1204. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1205. end
  1206. else
  1207. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1208. ungetregister(list,hr);
  1209. end;
  1210. procedure tcg.a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1211. var
  1212. hr : tregister;
  1213. hs : tmmshuffle;
  1214. begin
  1215. hr:=getmmregister(list,size);
  1216. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1217. if realshuffle(shuffle) then
  1218. begin
  1219. hs:=shuffle^;
  1220. removeshuffles(hs);
  1221. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1222. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1223. end
  1224. else
  1225. begin
  1226. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1227. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1228. end;
  1229. ungetregister(list,hr);
  1230. end;
  1231. procedure tcg.a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1232. begin
  1233. case loc.loc of
  1234. LOC_CMMREGISTER,LOC_MMREGISTER:
  1235. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1236. LOC_CREFERENCE,LOC_REFERENCE:
  1237. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1238. else
  1239. internalerror(200312232);
  1240. end;
  1241. end;
  1242. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  1243. begin
  1244. reg_cgsize := OS_INT;
  1245. end;
  1246. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  1247. var
  1248. paraloc1,paraloc2,paraloc3 : tparalocation;
  1249. begin
  1250. {$ifdef FPC}
  1251. {$warning FIX ME!}
  1252. {$endif}
  1253. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1254. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1255. paraloc3:=paramanager.getintparaloc(pocall_default,3);
  1256. paramanager.allocparaloc(list,paraloc3);
  1257. a_paramaddr_ref(list,dest,paraloc3);
  1258. paramanager.allocparaloc(list,paraloc2);
  1259. if loadref then
  1260. a_param_ref(list,OS_ADDR,source,paraloc2)
  1261. else
  1262. a_paramaddr_ref(list,source,paraloc2);
  1263. if delsource then
  1264. reference_release(list,source);
  1265. paramanager.allocparaloc(list,paraloc1);
  1266. a_param_const(list,OS_INT,len,paraloc1);
  1267. paramanager.freeparaloc(list,paraloc3);
  1268. paramanager.freeparaloc(list,paraloc2);
  1269. paramanager.freeparaloc(list,paraloc1);
  1270. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1271. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  1272. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1273. end;
  1274. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  1275. var
  1276. href : treference;
  1277. incrfunc : string;
  1278. paraloc1,paraloc2 : tparalocation;
  1279. begin
  1280. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1281. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1282. { These functions should not change the registers (they use
  1283. the saveregister proc directive }
  1284. if is_interfacecom(t) then
  1285. incrfunc:='FPC_INTF_INCR_REF'
  1286. else if is_ansistring(t) then
  1287. incrfunc:='FPC_ANSISTR_INCR_REF'
  1288. else if is_widestring(t) then
  1289. incrfunc:='FPC_WIDESTR_INCR_REF'
  1290. else if is_dynamic_array(t) then
  1291. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1292. else
  1293. incrfunc:='';
  1294. { call the special incr function or the generic addref }
  1295. if incrfunc<>'' then
  1296. begin
  1297. { these functions get the pointer by value }
  1298. paramanager.allocparaloc(list,paraloc1);
  1299. a_param_ref(list,OS_ADDR,ref,paraloc1);
  1300. paramanager.freeparaloc(list,paraloc1);
  1301. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1302. a_call_name(list,incrfunc);
  1303. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1304. end
  1305. else
  1306. begin
  1307. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1308. paramanager.allocparaloc(list,paraloc2);
  1309. a_paramaddr_ref(list,href,paraloc2);
  1310. paramanager.allocparaloc(list,paraloc1);
  1311. if loadref then
  1312. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1313. else
  1314. a_paramaddr_ref(list,ref,paraloc1);
  1315. paramanager.freeparaloc(list,paraloc1);
  1316. paramanager.freeparaloc(list,paraloc2);
  1317. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1318. a_call_name(list,'FPC_ADDREF');
  1319. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1320. end;
  1321. end;
  1322. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference; loadref:boolean);
  1323. var
  1324. href : treference;
  1325. decrfunc : string;
  1326. needrtti : boolean;
  1327. paraloc1,paraloc2 : tparalocation;
  1328. begin
  1329. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1330. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1331. needrtti:=false;
  1332. if is_interfacecom(t) then
  1333. decrfunc:='FPC_INTF_DECR_REF'
  1334. else if is_ansistring(t) then
  1335. decrfunc:='FPC_ANSISTR_DECR_REF'
  1336. else if is_widestring(t) then
  1337. decrfunc:='FPC_WIDESTR_DECR_REF'
  1338. else if is_dynamic_array(t) then
  1339. begin
  1340. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1341. needrtti:=true;
  1342. end
  1343. else
  1344. decrfunc:='';
  1345. { call the special decr function or the generic decref }
  1346. if decrfunc<>'' then
  1347. begin
  1348. if needrtti then
  1349. begin
  1350. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1351. paramanager.allocparaloc(list,paraloc2);
  1352. a_paramaddr_ref(list,href,paraloc2);
  1353. end;
  1354. paramanager.allocparaloc(list,paraloc1);
  1355. if loadref then
  1356. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1357. else
  1358. a_paramaddr_ref(list,ref,paraloc1);
  1359. paramanager.freeparaloc(list,paraloc1);
  1360. if needrtti then
  1361. paramanager.freeparaloc(list,paraloc2);
  1362. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1363. a_call_name(list,decrfunc);
  1364. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1365. end
  1366. else
  1367. begin
  1368. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1369. paramanager.allocparaloc(list,paraloc2);
  1370. a_paramaddr_ref(list,href,paraloc2);
  1371. paramanager.allocparaloc(list,paraloc1);
  1372. if loadref then
  1373. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1374. else
  1375. a_paramaddr_ref(list,ref,paraloc1);
  1376. paramanager.freeparaloc(list,paraloc1);
  1377. paramanager.freeparaloc(list,paraloc2);
  1378. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1379. a_call_name(list,'FPC_DECREF');
  1380. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1381. end;
  1382. end;
  1383. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1384. var
  1385. href : treference;
  1386. paraloc1,paraloc2 : tparalocation;
  1387. begin
  1388. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1389. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1390. if is_ansistring(t) or
  1391. is_widestring(t) or
  1392. is_interfacecom(t) then
  1393. a_load_const_ref(list,OS_ADDR,0,ref)
  1394. else
  1395. begin
  1396. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1397. paramanager.allocparaloc(list,paraloc2);
  1398. a_paramaddr_ref(list,href,paraloc2);
  1399. paramanager.allocparaloc(list,paraloc1);
  1400. if loadref then
  1401. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1402. else
  1403. a_paramaddr_ref(list,ref,paraloc1);
  1404. paramanager.freeparaloc(list,paraloc1);
  1405. paramanager.freeparaloc(list,paraloc2);
  1406. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1407. a_call_name(list,'FPC_INITIALIZE');
  1408. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1409. end;
  1410. end;
  1411. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1412. var
  1413. href : treference;
  1414. paraloc1,paraloc2 : tparalocation;
  1415. begin
  1416. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1417. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1418. if is_ansistring(t) or
  1419. is_widestring(t) or
  1420. is_interfacecom(t) then
  1421. g_decrrefcount(list,t,ref,loadref)
  1422. else
  1423. begin
  1424. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1425. paramanager.allocparaloc(list,paraloc2);
  1426. a_paramaddr_ref(list,href,paraloc2);
  1427. paramanager.allocparaloc(list,paraloc1);
  1428. if loadref then
  1429. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1430. else
  1431. a_paramaddr_ref(list,ref,paraloc1);
  1432. paramanager.freeparaloc(list,paraloc1);
  1433. paramanager.freeparaloc(list,paraloc2);
  1434. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1435. a_call_name(list,'FPC_FINALIZE');
  1436. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1437. end;
  1438. end;
  1439. procedure tcg.g_rangecheck(list: taasmoutput; const l:tlocation;fromdef,todef: tdef);
  1440. { generate range checking code for the value at location p. The type }
  1441. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1442. { is the original type used at that location. When both defs are equal }
  1443. { the check is also insert (needed for succ,pref,inc,dec) }
  1444. const
  1445. {$ifdef ver1_0}
  1446. awordsignedmax=high(longint);
  1447. {$else}
  1448. awordsignedmax=high(aword) div 2;
  1449. {$endif}
  1450. var
  1451. neglabel : tasmlabel;
  1452. hreg : tregister;
  1453. lto,hto,
  1454. lfrom,hfrom : TConstExprInt;
  1455. from_signed: boolean;
  1456. begin
  1457. { range checking on and range checkable value? }
  1458. if not(cs_check_range in aktlocalswitches) or
  1459. not(fromdef.deftype in [orddef,enumdef,arraydef]) then
  1460. exit;
  1461. if is_64bit(fromdef) or is_64bit(todef) then
  1462. begin
  1463. cg64.g_rangecheck64(list,l,fromdef,todef);
  1464. exit;
  1465. end;
  1466. { only check when assigning to scalar, subranges are different, }
  1467. { when todef=fromdef then the check is always generated }
  1468. getrange(fromdef,lfrom,hfrom);
  1469. getrange(todef,lto,hto);
  1470. { no range check if from and to are equal and are both longint/dword }
  1471. { (if we have a 32bit processor) or int64/qword, since such }
  1472. { operations can at most cause overflows (JM) }
  1473. { Note that these checks are mostly processor independent, they only }
  1474. { have to be changed once we introduce 64bit subrange types }
  1475. if (fromdef = todef) and
  1476. (fromdef.deftype=orddef) and
  1477. (((sizeof(aword) = 4) and
  1478. (((torddef(fromdef).typ = s32bit) and
  1479. (lfrom = low(longint)) and
  1480. (hfrom = high(longint))) or
  1481. ((torddef(fromdef).typ = u32bit) and
  1482. (lfrom = low(cardinal)) and
  1483. (hfrom = high(cardinal)))))) then
  1484. exit;
  1485. if todef<>fromdef then
  1486. begin
  1487. { if the from-range falls completely in the to-range, no check }
  1488. { is necessary }
  1489. if (lto<=lfrom) and (hto>=hfrom) then
  1490. exit;
  1491. end;
  1492. { generate the rangecheck code for the def where we are going to }
  1493. { store the result }
  1494. { use the trick that }
  1495. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1496. { To be able to do that, we have to make sure however that either }
  1497. { fromdef and todef are both signed or unsigned, or that we leave }
  1498. { the parts < 0 and > maxlongint out }
  1499. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1500. from_signed := is_signed(fromdef);
  1501. if from_signed xor is_signed(todef) then
  1502. if from_signed then
  1503. { from is signed, to is unsigned }
  1504. begin
  1505. { if high(from) < 0 -> always range error }
  1506. if (hfrom < 0) or
  1507. { if low(to) > maxlongint also range error }
  1508. (lto > awordsignedmax) then
  1509. begin
  1510. a_call_name(list,'FPC_RANGEERROR');
  1511. exit
  1512. end;
  1513. { from is signed and to is unsigned -> when looking at from }
  1514. { as an unsigned value, it must be < maxlongint (otherwise }
  1515. { it's negative, which is invalid since "to" is unsigned) }
  1516. if hto > awordsignedmax then
  1517. hto := awordsignedmax;
  1518. end
  1519. else
  1520. { from is unsigned, to is signed }
  1521. begin
  1522. if (lfrom > awordsignedmax) or
  1523. (hto < 0) then
  1524. begin
  1525. a_call_name(list,'FPC_RANGEERROR');
  1526. exit
  1527. end;
  1528. { from is unsigned and to is signed -> when looking at to }
  1529. { as an unsigned value, it must be >= 0 (since negative }
  1530. { values are the same as values > maxlongint) }
  1531. if lto < 0 then
  1532. lto := 0;
  1533. end;
  1534. hreg:=getintregister(list,OS_INT);
  1535. a_load_loc_reg(list,OS_INT,l,hreg);
  1536. a_op_const_reg(list,OP_SUB,OS_INT,aword(lto),hreg);
  1537. objectlibrary.getlabel(neglabel);
  1538. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1539. { !!! should happen right after the compare (JM) }
  1540. ungetregister(list,hreg);
  1541. a_call_name(list,'FPC_RANGEERROR');
  1542. a_label(list,neglabel);
  1543. end;
  1544. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1545. var
  1546. paraloc1 : tparalocation;
  1547. begin
  1548. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1549. paramanager.allocparaloc(list,paraloc1);
  1550. a_param_const(list,OS_32,stackframesize,paraloc1);
  1551. paramanager.freeparaloc(list,paraloc1);
  1552. { No register saving needed, saveregisters is used }
  1553. {$ifndef x86}
  1554. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1555. {$endif x86}
  1556. a_call_name(list,'FPC_STACKCHECK');
  1557. {$ifndef x86}
  1558. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1559. {$endif x86}
  1560. end;
  1561. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1562. var
  1563. tmpreg : tregister;
  1564. begin
  1565. tmpreg:=getintregister(list,size);
  1566. g_flags2reg(list,size,f,tmpreg);
  1567. a_load_reg_ref(list,size,size,tmpreg,ref);
  1568. ungetregister(list,tmpreg);
  1569. end;
  1570. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1571. var
  1572. OKLabel : tasmlabel;
  1573. paraloc1 : tparalocation;
  1574. begin
  1575. if (cs_check_object in aktlocalswitches) or
  1576. (cs_check_range in aktlocalswitches) then
  1577. begin
  1578. objectlibrary.getlabel(oklabel);
  1579. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1580. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1581. paramanager.allocparaloc(list,paraloc1);
  1582. a_param_const(list,OS_INT,210,paraloc1);
  1583. paramanager.freeparaloc(list,paraloc1);
  1584. a_call_name(list,'FPC_HANDLEERROR');
  1585. a_label(list,oklabel);
  1586. end;
  1587. end;
  1588. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1589. var
  1590. hrefvmt : treference;
  1591. paraloc1,paraloc2 : tparalocation;
  1592. begin
  1593. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1594. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1595. if (cs_check_object in aktlocalswitches) then
  1596. begin
  1597. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymboldata(objdef.vmt_mangledname),0);
  1598. paramanager.allocparaloc(list,paraloc2);
  1599. a_paramaddr_ref(list,hrefvmt,paraloc2);
  1600. paramanager.allocparaloc(list,paraloc1);
  1601. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1602. paramanager.freeparaloc(list,paraloc1);
  1603. paramanager.freeparaloc(list,paraloc2);
  1604. { No register saving needed, saveregisters is used }
  1605. {$ifndef x86}
  1606. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1607. {$endif x86}
  1608. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1609. {$ifndef x86}
  1610. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1611. {$endif x86}
  1612. end
  1613. else
  1614. if (cs_check_range in aktlocalswitches) then
  1615. begin
  1616. paramanager.allocparaloc(list,paraloc1);
  1617. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1618. paramanager.freeparaloc(list,paraloc1);
  1619. { No register saving needed, saveregisters is used }
  1620. {$ifndef x86}
  1621. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1622. {$endif x86}
  1623. a_call_name(list,'FPC_CHECK_OBJECT');
  1624. {$ifndef x86}
  1625. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1626. {$endif x86}
  1627. end;
  1628. end;
  1629. {*****************************************************************************
  1630. Entry/Exit Code Functions
  1631. *****************************************************************************}
  1632. procedure tcg.g_releasevaluepara_openarray(list : taasmoutput;const ref:treference);
  1633. begin
  1634. end;
  1635. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1636. begin
  1637. end;
  1638. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);
  1639. begin
  1640. end;
  1641. procedure tcg.g_profilecode(list : taasmoutput);
  1642. begin
  1643. end;
  1644. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1645. begin
  1646. a_load_reg_ref(list, OS_S32, OS_32, NR_FUNCTION_RETURN_REG, href);
  1647. end;
  1648. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1649. begin
  1650. a_load_const_ref(list, OS_S32, a, href);
  1651. end;
  1652. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1653. begin
  1654. a_load_ref_reg(list, OS_S32, OS_S32, href, NR_FUNCTION_RETURN_REG);
  1655. end;
  1656. {*****************************************************************************
  1657. TCG64
  1658. *****************************************************************************}
  1659. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword; regsrc,regdst : tregister64);
  1660. begin
  1661. a_load64_reg_reg(list,regsrc,regdst);
  1662. a_op64_const_reg(list,op,value,regdst);
  1663. end;
  1664. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1665. begin
  1666. a_load64_reg_reg(list,regsrc2,regdst);
  1667. a_op64_reg_reg(list,op,regsrc1,regdst);
  1668. end;
  1669. {****************************************************************************
  1670. TReference
  1671. ****************************************************************************}
  1672. procedure reference_reset(var ref : treference);
  1673. begin
  1674. FillChar(ref,sizeof(treference),0);
  1675. {$ifdef arm}
  1676. ref.signindex:=1;
  1677. {$endif arm}
  1678. end;
  1679. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  1680. begin
  1681. reference_reset(ref);
  1682. ref.base:=base;
  1683. ref.offset:=offset;
  1684. end;
  1685. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  1686. begin
  1687. reference_reset(ref);
  1688. ref.symbol:=sym;
  1689. ref.offset:=offset;
  1690. end;
  1691. procedure reference_release(list: taasmoutput; const ref : treference);
  1692. begin
  1693. cg.ungetreference(list,ref);
  1694. end;
  1695. function references_equal(sref : treference;dref : treference):boolean;
  1696. begin
  1697. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  1698. end;
  1699. {****************************************************************************
  1700. TLocation
  1701. ****************************************************************************}
  1702. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  1703. begin
  1704. FillChar(l,sizeof(tlocation),0);
  1705. l.loc:=lt;
  1706. l.size:=lsize;
  1707. {$ifdef arm}
  1708. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  1709. l.reference.signindex:=1;
  1710. {$endif arm}
  1711. end;
  1712. procedure location_release(list: taasmoutput; const l : tlocation);
  1713. begin
  1714. case l.loc of
  1715. LOC_REGISTER,LOC_CREGISTER :
  1716. begin
  1717. cg.ungetregister(list,l.register);
  1718. if l.size in [OS_64,OS_S64] then
  1719. cg.ungetregister(list,l.registerhigh);
  1720. end;
  1721. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  1722. cg.ungetregister(list,l.register);
  1723. LOC_CREFERENCE,LOC_REFERENCE :
  1724. cg.ungetreference(list, l.reference);
  1725. end;
  1726. end;
  1727. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  1728. begin
  1729. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1730. tg.ungetiftemp(list,l.reference);
  1731. end;
  1732. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  1733. begin
  1734. destloc:=sourceloc;
  1735. end;
  1736. procedure location_swap(var destloc,sourceloc : tlocation);
  1737. var
  1738. swapl : tlocation;
  1739. begin
  1740. swapl := destloc;
  1741. destloc := sourceloc;
  1742. sourceloc := swapl;
  1743. end;
  1744. initialization
  1745. ;
  1746. finalization
  1747. cg.free;
  1748. cg64.free;
  1749. end.
  1750. {
  1751. $Log$
  1752. Revision 1.146 2003-12-26 14:02:30 peter
  1753. * sparc updates
  1754. * use registertype in spill_register
  1755. Revision 1.145 2003/12/26 13:19:16 florian
  1756. * rtl and compiler compile with -Cfsse2
  1757. Revision 1.144 2003/12/24 00:10:02 florian
  1758. - delete parameter in cg64 methods removed
  1759. Revision 1.143 2003/12/23 14:38:07 florian
  1760. + second_floataddsse implemented
  1761. Revision 1.142 2003/12/22 19:00:17 florian
  1762. * fixed some x86-64 issues
  1763. Revision 1.141 2003/12/21 19:42:42 florian
  1764. * fixed ppc inlining stuff
  1765. * fixed wrong unit writing
  1766. + added some sse stuff
  1767. Revision 1.140 2003/12/15 21:39:39 florian
  1768. * improved register allocation of generic a_param_const and a_param_ref
  1769. Revision 1.139 2003/12/15 21:25:48 peter
  1770. * reg allocations for imaginary register are now inserted just
  1771. before reg allocation
  1772. * tregister changed to enum to allow compile time check
  1773. * fixed several tregister-tsuperregister errors
  1774. Revision 1.138 2003/12/12 17:16:17 peter
  1775. * rg[tregistertype] added in tcg
  1776. Revision 1.137 2003/12/06 22:11:47 jonas
  1777. + allocate volatile registers around calls to procedures declared with
  1778. "saveregisters" on non-x86 processors
  1779. Revision 1.136 2003/12/06 01:15:22 florian
  1780. * reverted Peter's alloctemp patch; hopefully properly
  1781. Revision 1.135 2003/12/03 23:13:19 peter
  1782. * delayed paraloc allocation, a_param_*() gets extra parameter
  1783. if it needs to allocate temp or real paralocation
  1784. * optimized/simplified int-real loading
  1785. Revision 1.134 2003/11/05 23:05:13 florian
  1786. * elesize of g_copyvaluepara_openarray changed
  1787. + g_releaesvaluepara_openarray added
  1788. Revision 1.133 2003/10/19 01:34:30 florian
  1789. * some ppc stuff fixed
  1790. * memory leak fixed
  1791. Revision 1.132 2003/10/17 15:25:18 florian
  1792. * fixed more ppc stuff
  1793. Revision 1.131 2003/10/17 14:38:32 peter
  1794. * 64k registers supported
  1795. * fixed some memory leaks
  1796. Revision 1.130 2003/10/13 01:23:13 florian
  1797. * some ideas for mm support implemented
  1798. Revision 1.129 2003/10/11 16:06:42 florian
  1799. * fixed some MMX<->SSE
  1800. * started to fix ppc, needs an overhaul
  1801. + stabs info improve for spilling, not sure if it works correctly/completly
  1802. - MMX_SUPPORT removed from Makefile.fpc
  1803. Revision 1.128 2003/10/10 17:48:13 peter
  1804. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1805. * tregisteralloctor renamed to trgobj
  1806. * removed rgobj from a lot of units
  1807. * moved location_* and reference_* to cgobj
  1808. * first things for mmx register allocation
  1809. Revision 1.127 2003/10/09 21:31:37 daniel
  1810. * Register allocator splitted, ans abstract now
  1811. Revision 1.126 2003/10/01 20:34:48 peter
  1812. * procinfo unit contains tprocinfo
  1813. * cginfo renamed to cgbase
  1814. * moved cgmessage to verbose
  1815. * fixed ppc and sparc compiles
  1816. Revision 1.125 2003/09/28 17:55:03 peter
  1817. * parent framepointer changed to hidden parameter
  1818. * tloadparentfpnode added
  1819. Revision 1.124 2003/09/28 13:40:13 peter
  1820. * a_call_ref removed
  1821. Revision 1.123 2003/09/25 21:26:24 peter
  1822. * remove obsolete tparalocation.sp_fixup
  1823. Revision 1.122 2003/09/23 20:37:16 peter
  1824. * fpc_check_object(_ext) has saveregisters and doesn't need
  1825. saving of registers
  1826. Revision 1.121 2003/09/10 08:31:47 marco
  1827. * Patch from Peter for paraloc
  1828. Revision 1.120 2003/09/09 20:59:27 daniel
  1829. * Adding register allocation order
  1830. Revision 1.119 2003/09/07 22:09:34 peter
  1831. * preparations for different default calling conventions
  1832. * various RA fixes
  1833. Revision 1.118 2003/09/03 15:55:00 peter
  1834. * NEWRA branch merged
  1835. Revision 1.117 2003/09/03 11:18:36 florian
  1836. * fixed arm concatcopy
  1837. + arm support in the common compiler sources added
  1838. * moved some generic cg code around
  1839. + tfputype added
  1840. * ...
  1841. Revision 1.116.2.4 2003/08/29 17:28:59 peter
  1842. * next batch of updates
  1843. Revision 1.116.2.3 2003/08/28 18:35:07 peter
  1844. * tregister changed to cardinal
  1845. Revision 1.116.2.2 2003/08/27 20:23:55 peter
  1846. * remove old ra code
  1847. Revision 1.116.2.1 2003/08/27 19:55:54 peter
  1848. * first tregister patch
  1849. Revision 1.116 2003/08/17 16:59:20 jonas
  1850. * fixed regvars so they work with newra (at least for ppc)
  1851. * fixed some volatile register bugs
  1852. + -dnotranslation option for -dnewra, which causes the registers not to
  1853. be translated from virtual to normal registers. Requires support in
  1854. the assembler writer as well, which is only implemented in aggas/
  1855. agppcgas currently
  1856. Revision 1.115 2003/07/23 11:01:14 jonas
  1857. * several rg.allocexplicitregistersint/rg.deallocexplicitregistersint
  1858. pairs round calls to helpers
  1859. Revision 1.114 2003/07/06 17:58:22 peter
  1860. * framepointer fixes for sparc
  1861. * parent framepointer code more generic
  1862. Revision 1.113 2003/07/02 22:18:04 peter
  1863. * paraloc splitted in callerparaloc,calleeparaloc
  1864. * sparc calling convention updates
  1865. Revision 1.112 2003/06/13 21:19:30 peter
  1866. * current_procdef removed, use current_procinfo.procdef instead
  1867. Revision 1.111 2003/06/12 21:11:10 peter
  1868. * ungetregisterfpu gets size parameter
  1869. Revision 1.110 2003/06/12 16:43:07 peter
  1870. * newra compiles for sparc
  1871. Revision 1.109 2003/06/07 18:57:04 jonas
  1872. + added freeintparaloc
  1873. * ppc get/freeintparaloc now check whether the parameter regs are
  1874. properly allocated/deallocated (and get an extra list para)
  1875. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1876. * fixed lot of missing pi_do_call's
  1877. Revision 1.108 2003/06/06 14:43:02 peter
  1878. * g_copyopenarrayvalue gets length reference
  1879. * don't copy open arrays for cdecl
  1880. Revision 1.107 2003/06/03 21:11:09 peter
  1881. * cg.a_load_* get a from and to size specifier
  1882. * makeregsize only accepts newregister
  1883. * i386 uses generic tcgnotnode,tcgunaryminus
  1884. Revision 1.106 2003/06/03 13:01:59 daniel
  1885. * Register allocator finished
  1886. Revision 1.105 2003/06/01 21:38:06 peter
  1887. * getregisterfpu size parameter added
  1888. * op_const_reg size parameter added
  1889. * sparc updates
  1890. Revision 1.104 2003/06/01 01:02:39 peter
  1891. * generic a_call_ref
  1892. Revision 1.103 2003/05/30 23:57:08 peter
  1893. * more sparc cleanup
  1894. * accumulator removed, splitted in function_return_reg (called) and
  1895. function_result_reg (caller)
  1896. Revision 1.102 2003/05/30 23:49:18 jonas
  1897. * a_load_loc_reg now has an extra size parameter for the destination
  1898. register (properly fixes what I worked around in revision 1.106 of
  1899. ncgutil.pas)
  1900. Revision 1.101 2003/05/30 21:40:00 jonas
  1901. * fixed bug in a_load_loc_ref (the source instead of dest size was passed
  1902. to a_load_reg_ref in case of a register)
  1903. Revision 1.100 2003/05/30 12:36:13 jonas
  1904. * use as little different registers on the ppc until newra is released,
  1905. since every used register must be saved
  1906. Revision 1.99 2003/05/23 14:27:35 peter
  1907. * remove some unit dependencies
  1908. * current_procinfo changes to store more info
  1909. Revision 1.98 2003/05/15 18:58:53 peter
  1910. * removed selfpointer_offset, vmtpointer_offset
  1911. * tvarsym.adjusted_address
  1912. * address in localsymtable is now in the real direction
  1913. * removed some obsolete globals
  1914. Revision 1.97 2003/05/13 19:14:41 peter
  1915. * failn removed
  1916. * inherited result code check moven to pexpr
  1917. Revision 1.96 2003/05/11 21:37:03 peter
  1918. * moved implicit exception frame from ncgutil to psub
  1919. * constructor/destructor helpers moved from cobj/ncgutil to psub
  1920. Revision 1.95 2003/05/09 17:47:02 peter
  1921. * self moved to hidden parameter
  1922. * removed hdisposen,hnewn,selfn
  1923. Revision 1.94 2003/05/01 12:23:46 jonas
  1924. * fix for op_reg_reg_reg in case the destination is the same as the first
  1925. source register
  1926. Revision 1.93 2003/04/29 07:28:52 michael
  1927. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1928. Revision 1.92 2003/04/27 11:21:32 peter
  1929. * aktprocdef renamed to current_procinfo.procdef
  1930. * procinfo renamed to current_procinfo
  1931. * procinfo will now be stored in current_module so it can be
  1932. cleaned up properly
  1933. * gen_main_procsym changed to create_main_proc and release_main_proc
  1934. to also generate a tprocinfo structure
  1935. * fixed unit implicit initfinal
  1936. Revision 1.91 2003/04/27 07:29:50 peter
  1937. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1938. a new procdef declaration
  1939. * aktprocsym removed
  1940. * lexlevel removed, use symtable.symtablelevel instead
  1941. * implicit init/final code uses the normal genentry/genexit
  1942. * funcret state checking updated for new funcret handling
  1943. Revision 1.90 2003/04/26 20:57:17 florian
  1944. * fixed para locations of fpc_class_new helper call
  1945. Revision 1.89 2003/04/26 17:21:08 florian
  1946. * fixed passing of fpu values by fpu register
  1947. Revision 1.88 2003/04/23 20:16:03 peter
  1948. + added currency support based on int64
  1949. + is_64bit for use in cg units instead of is_64bitint
  1950. * removed cgmessage from n386add, replace with internalerrors
  1951. Revision 1.87 2003/04/23 14:42:07 daniel
  1952. * Further register allocator work. Compiler now smaller with new
  1953. allocator than without.
  1954. * Somebody forgot to adjust ppu version number
  1955. Revision 1.86 2003/04/23 13:20:34 peter
  1956. * fix self passing to fpc_help_fail
  1957. Revision 1.85 2003/04/23 12:35:34 florian
  1958. * fixed several issues with powerpc
  1959. + applied a patch from Jonas for nested function calls (PowerPC only)
  1960. * ...
  1961. Revision 1.84 2003/04/22 14:33:38 peter
  1962. * removed some notes/hints
  1963. Revision 1.83 2003/04/22 13:47:08 peter
  1964. * fixed C style array of const
  1965. * fixed C array passing
  1966. * fixed left to right with high parameters
  1967. Revision 1.82 2003/04/22 10:09:34 daniel
  1968. + Implemented the actual register allocator
  1969. + Scratch registers unavailable when new register allocator used
  1970. + maybe_save/maybe_restore unavailable when new register allocator used
  1971. Revision 1.81 2003/04/06 21:11:23 olle
  1972. * changed newasmsymbol to newasmsymboldata for data symbols
  1973. Revision 1.80 2003/03/28 19:16:56 peter
  1974. * generic constructor working for i386
  1975. * remove fixed self register
  1976. * esi added as address register for i386
  1977. Revision 1.79 2003/03/22 18:07:18 jonas
  1978. + add used scratch registers to usedintbyproc for non-i386
  1979. Revision 1.78 2003/03/11 21:46:24 jonas
  1980. * lots of new regallocator fixes, both in generic and ppc-specific code
  1981. (ppc compiler still can't compile the linux system unit though)
  1982. Revision 1.77 2003/02/19 22:00:14 daniel
  1983. * Code generator converted to new register notation
  1984. - Horribily outdated todo.txt removed
  1985. Revision 1.76 2003/01/31 22:47:48 peter
  1986. * maybe_testself now really uses the passed register
  1987. Revision 1.75 2003/01/30 21:46:35 peter
  1988. * maybe_testvmt added
  1989. Revision 1.74 2003/01/17 12:45:40 daniel
  1990. * Fixed internalerror 200301081 problem
  1991. Revision 1.73 2003/01/13 14:54:34 daniel
  1992. * Further work to convert codegenerator register convention;
  1993. internalerror bug fixed.
  1994. Revision 1.72 2003/01/09 22:00:53 florian
  1995. * fixed some PowerPC issues
  1996. Revision 1.71 2003/01/09 20:41:10 florian
  1997. * fixed broken PowerPC compiler
  1998. Revision 1.70 2003/01/08 18:43:56 daniel
  1999. * Tregister changed into a record
  2000. Revision 1.69 2002/12/24 15:56:50 peter
  2001. * stackpointer_alloc added for adjusting ESP. Win32 needs
  2002. this for the pageprotection
  2003. Revision 1.68 2002/12/20 18:14:04 peter
  2004. * removed some runerror and writeln
  2005. Revision 1.67 2002/12/14 15:02:03 carl
  2006. * maxoperands -> max_operands (for portability in rautils.pas)
  2007. * fix some range-check errors with loadconst
  2008. + add ncgadd unit to m68k
  2009. * some bugfix of a_param_reg with LOC_CREFERENCE
  2010. Revision 1.66 2002/11/25 17:43:16 peter
  2011. * splitted defbase in defutil,symutil,defcmp
  2012. * merged isconvertable and is_equal into compare_defs(_ext)
  2013. * made operator search faster by walking the list only once
  2014. Revision 1.65 2002/11/17 16:27:31 carl
  2015. * document flags2reg
  2016. Revision 1.64 2002/11/16 17:06:28 peter
  2017. * return error 210 for failed self test
  2018. Revision 1.63 2002/11/15 01:58:46 peter
  2019. * merged changes from 1.0.7 up to 04-11
  2020. - -V option for generating bug report tracing
  2021. - more tracing for option parsing
  2022. - errors for cdecl and high()
  2023. - win32 import stabs
  2024. - win32 records<=8 are returned in eax:edx (turned off by default)
  2025. - heaptrc update
  2026. - more info for temp management in .s file with EXTDEBUG
  2027. Revision 1.62 2002/10/16 19:01:43 peter
  2028. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  2029. implicit exception frames for procedures with initialized variables
  2030. and for constructors. The default is on for compatibility
  2031. Revision 1.61 2002/10/05 12:43:23 carl
  2032. * fixes for Delphi 6 compilation
  2033. (warning : Some features do not work under Delphi)
  2034. Revision 1.60 2002/10/02 18:20:52 peter
  2035. * Copy() is now internal syssym that calls compilerprocs
  2036. Revision 1.59 2002/09/17 18:54:02 jonas
  2037. * a_load_reg_reg() now has two size parameters: source and dest. This
  2038. allows some optimizations on architectures that don't encode the
  2039. register size in the register name.
  2040. Revision 1.58 2002/09/09 19:29:29 peter
  2041. * fixed dynarr_decr_ref call
  2042. Revision 1.57 2002/09/07 15:25:01 peter
  2043. * old logs removed and tabs fixed
  2044. Revision 1.56 2002/09/01 21:04:47 florian
  2045. * several powerpc related stuff fixed
  2046. Revision 1.55 2002/09/01 17:05:43 florian
  2047. + added abstract tcg.g_removevaluepara_openarray
  2048. Revision 1.54 2002/09/01 12:09:27 peter
  2049. + a_call_reg, a_call_loc added
  2050. * removed exprasmlist references
  2051. Revision 1.53 2002/08/19 18:17:48 carl
  2052. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  2053. * more fixes to m68k for 64-bit operations
  2054. Revision 1.52 2002/08/17 22:09:43 florian
  2055. * result type handling in tcgcal.pass_2 overhauled
  2056. * better tnode.dowrite
  2057. * some ppc stuff fixed
  2058. Revision 1.51 2002/08/17 09:23:33 florian
  2059. * first part of procinfo rewrite
  2060. Revision 1.50 2002/08/16 14:24:57 carl
  2061. * issameref() to test if two references are the same (then emit no opcodes)
  2062. + ret_in_reg to replace ret_in_acc
  2063. (fix some register allocation bugs at the same time)
  2064. + save_std_register now has an extra parameter which is the
  2065. usedinproc registers
  2066. Revision 1.49 2002/08/15 08:13:54 carl
  2067. - a_load_sym_ofs_reg removed
  2068. * loadvmt now calls loadaddr_ref_reg instead
  2069. Revision 1.48 2002/08/14 19:26:02 carl
  2070. + routine to optimize opcodes with constants
  2071. Revision 1.47 2002/08/11 14:32:26 peter
  2072. * renamed current_library to objectlibrary
  2073. Revision 1.46 2002/08/11 13:24:11 peter
  2074. * saving of asmsymbols in ppu supported
  2075. * asmsymbollist global is removed and moved into a new class
  2076. tasmlibrarydata that will hold the info of a .a file which
  2077. corresponds with a single module. Added librarydata to tmodule
  2078. to keep the library info stored for the module. In the future the
  2079. objectfiles will also be stored to the tasmlibrarydata class
  2080. * all getlabel/newasmsymbol and friends are moved to the new class
  2081. Revision 1.45 2002/08/10 17:15:20 jonas
  2082. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  2083. Revision 1.44 2002/08/09 19:10:05 carl
  2084. - moved new_exception and free_exception to ncgutils
  2085. Revision 1.43 2002/08/05 18:27:48 carl
  2086. + more more more documentation
  2087. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2088. Revision 1.42 2002/08/04 19:08:21 carl
  2089. + added generic exception support (still does not work!)
  2090. + more documentation
  2091. Revision 1.41 2002/07/30 20:50:43 florian
  2092. * the code generator knows now if parameters are in registers
  2093. Revision 1.40 2002/07/29 21:16:02 florian
  2094. * some more ppc fixes
  2095. Revision 1.39 2002/07/28 15:56:00 jonas
  2096. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  2097. generic implementation
  2098. Revision 1.38 2002/07/27 19:53:51 jonas
  2099. + generic implementation of tcg.g_flags2ref()
  2100. * tcg.flags2xxx() now also needs a size parameter
  2101. Revision 1.37 2002/07/20 11:57:53 florian
  2102. * types.pas renamed to defbase.pas because D6 contains a types
  2103. unit so this would conflicts if D6 programms are compiled
  2104. + Willamette/SSE2 instructions to assembler added
  2105. }