cgobj.pas 84 KB

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