cgobj.pas 97 KB

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