cgobj.pas 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  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. cclasses,aasmbase,aasmtai,aasmcpu,symtable,
  32. cpubase,cpuinfo,cpupara,
  33. cginfo,
  34. symconst,symbase,symtype,node
  35. {$ifdef delphi}
  36. ,dmisc
  37. {$endif}
  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. scratch_register_array_pointer : aword;
  50. {# List of currently unused scratch registers }
  51. unusedscratchregisters : tregisterset;
  52. alignment : talignment;
  53. {************************************************}
  54. { basic routines }
  55. constructor create;
  56. { returns the tcgsize corresponding with the size of reg }
  57. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  58. {# Emit a label to the instruction stream. }
  59. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  60. {# Allocates register r by inserting a pai_realloc record }
  61. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  62. {# Deallocates register r by inserting a pa_regdealloc record}
  63. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  64. {# @abstract(Returns an int register for use as scratch register)
  65. This routine returns a register which can be used by
  66. the code generator as a general purpose scratch register.
  67. Since scratch_registers are scarce resources, the register
  68. should be freed by calling @link(free_scratch_reg) as
  69. soon as it is no longer required.
  70. }
  71. function get_scratch_reg_int(list : taasmoutput) : tregister;virtual;
  72. {# @abstract(Returns an address register for use as scratch register)
  73. This routine returns a register which can be used by
  74. the code generator as a pointer scratch register.
  75. Since scratch_registers are scarce resources, the register
  76. should be freed by calling @link(free_scratch_reg) as
  77. soon as it is no longer required.
  78. }
  79. function get_scratch_reg_address(list : taasmoutput) : tregister;virtual;
  80. {# @abstract(Releases a scratch register)
  81. Releases a scratch register.
  82. This routine is used to free a register which
  83. was previously allocated using @link(get_scratch_reg).
  84. }
  85. procedure free_scratch_reg(list : taasmoutput;r : tregister);
  86. {# Pass a parameter, which is located in a register, to a routine.
  87. This routine should push/send the parameter to the routine, as
  88. required by the specific processor ABI and routine modifiers.
  89. This must be overriden for each CPU target.
  90. @param(size size of the operand in the register)
  91. @param(r register source of the operand)
  92. @param(locpara where the parameter will be stored)
  93. }
  94. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);virtual;
  95. {# Pass a parameter, which is a constant, to a routine.
  96. A generic version is provided. This routine should
  97. be overriden for optimization purposes if the cpu
  98. permits directly sending this type of parameter.
  99. @param(size size of the operand in constant)
  100. @param(a value of constant to send)
  101. @param(locpara where the parameter will be stored)
  102. }
  103. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);virtual;
  104. {# Pass the value of a parameter, which is located in memory, to a routine.
  105. A generic version is provided. This routine should
  106. be overriden for optimization purposes if the cpu
  107. permits directly sending this type of parameter.
  108. @param(size size of the operand in constant)
  109. @param(r Memory reference of value to send)
  110. @param(locpara where the parameter will be stored)
  111. }
  112. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);virtual;
  113. {# Pass the value of a parameter, which can be located either in a register or memory location,
  114. to a routine.
  115. A generic version is provided.
  116. @param(l location of the operand to send)
  117. @param(nr parameter number (starting from one) of routine (from left to right))
  118. @param(locpara where the parameter will be stored)
  119. }
  120. procedure a_param_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);
  121. {# Pass the address of a reference to a routine. This routine
  122. will calculate the address of the reference, and pass this
  123. calculated address as a parameter.
  124. A generic version is provided. This routine should
  125. be overriden for optimization purposes if the cpu
  126. permits directly sending this type of parameter.
  127. @param(r reference to get address from)
  128. @param(nr parameter number (starting from one) of routine (from left to right))
  129. }
  130. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);virtual;
  131. { Copies a whole memory block to the stack, the locpara must be a memory location }
  132. procedure a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  133. (* Remarks:
  134. * If a method specifies a size you have only to take care
  135. of that number of bits, i.e. load_const_reg with OP_8 must
  136. only load the lower 8 bit of the specified register
  137. the rest of the register can be undefined
  138. if necessary the compiler will call a method
  139. to zero or sign extend the register
  140. * The a_load_XX_XX with OP_64 needn't to be
  141. implemented for 32 bit
  142. processors, the code generator takes care of that
  143. * the addr size is for work with the natural pointer
  144. size
  145. * the procedures without fpu/mm are only for integer usage
  146. * normally the first location is the source and the
  147. second the destination
  148. *)
  149. {# Emits instruction to call the method specified by symbol name.
  150. This routine must be overriden for each new target cpu.
  151. }
  152. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  153. procedure a_call_ref(list : taasmoutput;const ref : treference);virtual;abstract;
  154. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual;abstract;
  155. procedure a_call_loc(list : taasmoutput;const loc:tlocation);
  156. { move instructions }
  157. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  158. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  159. procedure a_load_const_loc(list : taasmoutput;a : aword;const loc : tlocation);
  160. procedure a_load_reg_ref(list : taasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  161. procedure a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  162. procedure a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  163. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  164. procedure a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);virtual;
  165. procedure a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  166. procedure a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  167. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  168. { fpu move instructions }
  169. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  170. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  171. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  172. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  173. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  174. procedure a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);virtual;
  175. { vector register move instructions }
  176. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  177. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); virtual; abstract;
  178. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); virtual; abstract;
  179. procedure a_parammm_reg(list: taasmoutput; reg: tregister); virtual; abstract;
  180. { basic arithmetic operations }
  181. { note: for operators which require only one argument (not, neg), use }
  182. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  183. { that in this case the *second* operand is used as both source and }
  184. { destination (JM) }
  185. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  186. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  187. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  188. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  189. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  190. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  191. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  192. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  193. { trinary operations for processors that support them, 'emulated' }
  194. { on others. None with "ref" arguments since I don't think there }
  195. { are any processors that support it (JM) }
  196. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  197. size: tcgsize; a: aword; src, dst: tregister); virtual;
  198. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  199. size: tcgsize; src1, src2, dst: tregister); virtual;
  200. { comparison operations }
  201. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  202. l : tasmlabel);virtual; abstract;
  203. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  204. l : tasmlabel); virtual;
  205. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  206. l : tasmlabel);
  207. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  208. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  209. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  210. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  211. l : tasmlabel);
  212. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  213. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  214. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  215. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  216. }
  217. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  218. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  219. {
  220. This routine tries to optimize the const_reg opcode, and should be
  221. called at the start of a_op_const_reg. It returns the actual opcode
  222. to emit, and the constant value to emit. If this routine returns
  223. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  224. @param(op The opcode to emit, returns the opcode which must be emitted)
  225. @param(a The constant which should be emitted, returns the constant which must
  226. be emitted)
  227. @param(reg The register to emit the opcode with, returns the register with
  228. which the opcode will be emitted)
  229. }
  230. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg: tregister): boolean;virtual;
  231. {#
  232. This routine is used in exception management nodes. It should
  233. save the exception reason currently in the accumulator. The
  234. save should be done either to a temp (pointed to by href).
  235. or on the stack (pushing the value on the stack).
  236. The size of the value to save is OS_S32. The default version
  237. saves the exception reason to a temp. memory area.
  238. }
  239. procedure g_exception_reason_save(list : taasmoutput; const href : treference);virtual;
  240. {#
  241. This routine is used in exception management nodes. It should
  242. save the exception reason constant. The
  243. save should be done either to a temp (pointed to by href).
  244. or on the stack (pushing the value on the stack).
  245. The size of the value to save is OS_S32. The default version
  246. saves the exception reason to a temp. memory area.
  247. }
  248. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);virtual;
  249. {#
  250. This routine is used in exception management nodes. It should
  251. load the exception reason to the accumulator. The saved value
  252. should either be in the temp. area (pointed to by href , href should
  253. *NOT* be freed) or on the stack (the value should be popped).
  254. The size of the value to save is OS_S32. The default version
  255. saves the exception reason to a temp. memory area.
  256. }
  257. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  258. procedure g_maybe_loadself(list : taasmoutput);virtual;
  259. procedure g_maybe_testself(list : taasmoutput);virtual;
  260. {# This should emit the opcode to copy len bytes from the source
  261. to destination, if loadref is true, it assumes that it first must load
  262. the source address from the memory location where
  263. source points to.
  264. It must be overriden for each new target processor.
  265. @param(source Source reference of copy)
  266. @param(dest Destination reference of copy)
  267. @param(delsource Indicates if the source reference's resources should be freed)
  268. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  269. }
  270. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  271. {# This should emit the opcode to a shortrstring from the source
  272. to destination, if loadref is true, it assumes that it first must load
  273. the source address from the memory location where
  274. source points to.
  275. @param(source Source reference of copy)
  276. @param(dest Destination reference of copy)
  277. @param(delsource Indicates if the source reference's resources should be freed)
  278. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  279. }
  280. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  281. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  282. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  283. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  284. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  285. {# Emits the call to the stack checking routine of
  286. the runtime library. The default behavior
  287. does not need to be modified, as it is generic
  288. for all platforms.
  289. @param(stackframesize Number of bytes which will be allocated on the stack)
  290. }
  291. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  292. {# Generates range checking code. It is to note
  293. that this routine does not need to be overriden,
  294. as it takes care of everything.
  295. @param(p Node which contains the value to check)
  296. @param(todef Type definition of node to range check)
  297. }
  298. procedure g_rangecheck(list: taasmoutput; const p: tnode;
  299. const todef: tdef); virtual;
  300. {# Generates overflow checking code for a node }
  301. procedure g_overflowcheck(list: taasmoutput; const p: tnode); virtual; abstract;
  302. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);virtual;abstract;
  303. procedure g_removevaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);virtual;abstract;
  304. {# Emits instructions which should be emitted when entering
  305. a routine declared as @var(interrupt). The default
  306. behavior does nothing, should be overriden as required.
  307. }
  308. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  309. {# Emits instructions which should be emitted when exiting
  310. a routine declared as @var(interrupt). The default
  311. behavior does nothing, should be overriden as required.
  312. }
  313. procedure g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;
  314. {# Emits instructions when compilation is done in profile
  315. mode (this is set as a command line option). The default
  316. behavior does nothing, should be overriden as required.
  317. }
  318. procedure g_profilecode(list : taasmoutput);virtual;
  319. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  320. @param(size Number of bytes to allocate)
  321. }
  322. procedure g_stackpointer_alloc(list : taasmoutput;size : longint);virtual; abstract;
  323. {# Emits instruction for allocating the locals in entry
  324. code of a routine. This is one of the first
  325. routine called in @var(genentrycode).
  326. @param(localsize Number of bytes to allocate as locals)
  327. }
  328. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  329. {# Emits instructiona for restoring the frame pointer
  330. at routine exit. For some processors, this routine
  331. may do nothing at all.
  332. }
  333. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  334. {# Emits instructions for returning from a subroutine.
  335. Should also restore the stack.
  336. @param(parasize Number of bytes of parameters to deallocate from stack)
  337. }
  338. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  339. procedure g_call_constructor_helper(list : taasmoutput);virtual;
  340. procedure g_call_destructor_helper(list : taasmoutput);virtual;
  341. procedure g_call_fail_helper(list : taasmoutput);virtual;
  342. {# This routine is called when generating the code for the entry point
  343. of a routine. It should save all registers which are not used in this
  344. routine, and which should be declared as saved in the std_saved_registers
  345. set.
  346. This routine is mainly used when linking to code which is generated
  347. by ABI-compliant compilers (like GCC), to make sure that the reserved
  348. registers of that ABI are not clobbered.
  349. @param(usedinproc Registers which are used in the code of this routine)
  350. }
  351. procedure g_save_standard_registers(list : taasmoutput; usedinproc : tregisterset);virtual;abstract;
  352. {# This routine is called when generating the code for the exit point
  353. of a routine. It should restore all registers which were previously
  354. saved in @var(g_save_standard_registers).
  355. @param(usedinproc Registers which are used in the code of this routine)
  356. }
  357. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : tregisterset);virtual;abstract;
  358. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  359. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;abstract;
  360. end;
  361. {# @abstract(Abstract code generator for 64 Bit operations)
  362. This class implements an abstract code generator class
  363. for 64 Bit operations.
  364. }
  365. tcg64 = class
  366. { Allocates 64 Bit register r by inserting a pai_realloc record }
  367. procedure a_reg_alloc(list : taasmoutput;r : tregister64);virtual;abstract;
  368. { Deallocates 64 Bit register r by inserting a pa_regdealloc record}
  369. procedure a_reg_dealloc(list : taasmoutput;r : tregister64);virtual;abstract;
  370. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);virtual;abstract;
  371. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  372. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  373. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  374. procedure a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);virtual;abstract;
  375. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  376. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  377. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);virtual;abstract;
  378. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  379. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  380. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  381. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  382. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  383. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  384. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  385. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  386. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  387. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  388. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;regdst : tregister64);virtual;abstract;
  389. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);virtual;abstract;
  390. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);virtual;abstract;
  391. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  392. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  393. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);virtual;
  394. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  395. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  396. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  397. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  398. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  399. {
  400. This routine tries to optimize the const_reg opcode, and should be
  401. called at the start of a_op64_const_reg. It returns the actual opcode
  402. to emit, and the constant value to emit. If this routine returns
  403. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  404. @param(op The opcode to emit, returns the opcode which must be emitted)
  405. @param(a The constant which should be emitted, returns the constant which must
  406. be emitted)
  407. @param(reg The register to emit the opcode with, returns the register with
  408. which the opcode will be emitted)
  409. }
  410. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;virtual;abstract;
  411. { override to catch 64bit rangechecks }
  412. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  413. const todef: tdef);virtual;abstract;
  414. end;
  415. var
  416. {# Main code generator class }
  417. cg : tcg;
  418. {# Code generator class for all operations working with 64-Bit operands }
  419. cg64 : tcg64;
  420. implementation
  421. uses
  422. globals,globtype,options,systems,cgbase,
  423. verbose,defutil,tgobj,symdef,paramgr,
  424. rgobj,cutils;
  425. const
  426. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  427. {*****************************************************************************
  428. basic functionallity
  429. ******************************************************************************}
  430. constructor tcg.create;
  431. var
  432. i : longint;
  433. begin
  434. scratch_register_array_pointer:=1;
  435. for i:=low(scratch_regs) to high(scratch_regs) do
  436. include(unusedscratchregisters,scratch_regs[i]);
  437. end;
  438. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  439. begin
  440. list.concat(tai_regalloc.alloc(r));
  441. end;
  442. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  443. begin
  444. list.concat(tai_regalloc.dealloc(r));
  445. end;
  446. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  447. begin
  448. list.concat(tai_label.create(l));
  449. end;
  450. function tcg.get_scratch_reg_int(list : taasmoutput) : tregister;
  451. var
  452. r : tregister;
  453. i : longint;
  454. begin
  455. if unusedscratchregisters=[] then
  456. internalerror(68996);
  457. for i:=scratch_register_array_pointer to
  458. (scratch_register_array_pointer+max_scratch_regs-1) do
  459. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  460. begin
  461. r.enum:=scratch_regs[(i mod max_scratch_regs)+1];
  462. break;
  463. end;
  464. exclude(unusedscratchregisters,r.enum);
  465. inc(scratch_register_array_pointer);
  466. if scratch_register_array_pointer>max_scratch_regs then
  467. scratch_register_array_pointer:=1;
  468. a_reg_alloc(list,r);
  469. get_scratch_reg_int:=r;
  470. end;
  471. { the default behavior simply returns a general purpose register }
  472. function tcg.get_scratch_reg_address(list : taasmoutput) : tregister;
  473. begin
  474. get_scratch_reg_address := get_scratch_reg_int(list);
  475. end;
  476. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  477. begin
  478. include(unusedscratchregisters,rg.makeregsize(r,OS_INT).enum);
  479. a_reg_dealloc(list,r);
  480. end;
  481. {*****************************************************************************
  482. for better code generation these methods should be overridden
  483. ******************************************************************************}
  484. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  485. var
  486. ref : treference;
  487. t : Tregister;
  488. begin
  489. case locpara.loc of
  490. LOC_REGISTER,LOC_CREGISTER:
  491. a_load_reg_reg(list,size,locpara.size,r,locpara.register);
  492. LOC_REFERENCE,LOC_CREFERENCE:
  493. begin
  494. if locpara.sp_fixup<>0 then
  495. begin
  496. t.enum:=stack_pointer_reg;
  497. a_op_const_reg(list,OP_ADD,locpara.sp_fixup,t);
  498. end;
  499. reference_reset(ref);
  500. ref.base:=locpara.reference.index;
  501. ref.offset:=locpara.reference.offset;
  502. a_load_reg_ref(list,size,r,ref);
  503. end
  504. else
  505. internalerror(2002071004);
  506. end;
  507. end;
  508. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  509. var
  510. hr : tregister;
  511. begin
  512. hr:=get_scratch_reg_int(list);
  513. a_load_const_reg(list,size,a,hr);
  514. a_param_reg(list,size,hr,locpara);
  515. free_scratch_reg(list,hr);
  516. end;
  517. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  518. var
  519. hr : tregister;
  520. begin
  521. hr:=get_scratch_reg_int(list);
  522. a_load_ref_reg(list,size,r,hr);
  523. a_param_reg(list,size,hr,locpara);
  524. free_scratch_reg(list,hr);
  525. end;
  526. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  527. begin
  528. case l.loc of
  529. LOC_REGISTER,
  530. LOC_CREGISTER :
  531. a_param_reg(list,l.size,l.register,locpara);
  532. LOC_CONSTANT :
  533. a_param_const(list,l.size,l.value,locpara);
  534. LOC_CREFERENCE,
  535. LOC_REFERENCE :
  536. a_param_ref(list,l.size,l.reference,locpara);
  537. else
  538. internalerror(2002032211);
  539. end;
  540. end;
  541. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  542. var
  543. hr : tregister;
  544. begin
  545. hr:=get_scratch_reg_address(list);
  546. a_loadaddr_ref_reg(list,r,hr);
  547. a_param_reg(list,OS_ADDR,hr,locpara);
  548. free_scratch_reg(list,hr);
  549. end;
  550. procedure tcg.a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  551. var
  552. ref : treference;
  553. hr : tregister;
  554. begin
  555. if not(locpara.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  556. internalerror(2003010901);
  557. if locpara.sp_fixup<>0 then
  558. cg.g_stackpointer_alloc(list,locpara.sp_fixup);
  559. reference_reset(ref);
  560. ref.base:=locpara.reference.index;
  561. ref.offset:=locpara.reference.offset;
  562. cg.g_concatcopy(list,r,ref,size,false,false);
  563. end;
  564. {****************************************************************************
  565. some generic implementations
  566. ****************************************************************************}
  567. procedure tcg.a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);
  568. var
  569. tmpreg: tregister;
  570. {$ifdef i386}
  571. pushed_reg: tregister;
  572. {$endif i386}
  573. begin
  574. { verify if we have the same reference }
  575. if references_equal(sref,dref) then
  576. exit;
  577. {$ifdef i386}
  578. { the following is done with defines to avoid a speed penalty, }
  579. { since all this is only necessary for the 80x86 (because EDI }
  580. { doesn't have an 8bit component which is directly addressable) }
  581. pushed_reg.enum := R_NO;
  582. if size in [OS_8,OS_S8] then
  583. if (rg.countunusedregsint = 0) then
  584. begin
  585. if dref.base.enum>lastreg then
  586. internalerror(200301081);
  587. if dref.index.enum>lastreg then
  588. internalerror(200301081);
  589. if (dref.base.enum <> R_EBX) and
  590. (dref.index.enum <> R_EBX) then
  591. pushed_reg.enum := R_EBX
  592. else if (dref.base.enum <> R_EAX) and
  593. (dref.index.enum <> R_EAX) then
  594. pushed_reg.enum := R_EAX
  595. else pushed_reg.enum := R_ECX;
  596. tmpreg := rg.makeregsize(pushed_reg,OS_8);
  597. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  598. end
  599. else
  600. tmpreg := rg.getregisterint(list)
  601. else
  602. {$endif i386}
  603. tmpreg := get_scratch_reg_int(list);
  604. tmpreg:=rg.makeregsize(tmpreg,size);
  605. a_load_ref_reg(list,size,sref,tmpreg);
  606. a_load_reg_ref(list,size,tmpreg,dref);
  607. {$ifdef i386}
  608. if size in [OS_8,OS_S8] then
  609. begin
  610. if (pushed_reg.enum <> R_NO) then
  611. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  612. else
  613. rg.ungetregister(list,tmpreg)
  614. end
  615. else
  616. {$endif i386}
  617. free_scratch_reg(list,tmpreg);
  618. end;
  619. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  620. var
  621. tmpreg: tregister;
  622. begin
  623. tmpreg := get_scratch_reg_int(list);
  624. a_load_const_reg(list,size,a,tmpreg);
  625. a_load_reg_ref(list,size,tmpreg,ref);
  626. free_scratch_reg(list,tmpreg);
  627. end;
  628. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  629. begin
  630. case loc.loc of
  631. LOC_REFERENCE,LOC_CREFERENCE:
  632. a_load_const_ref(list,loc.size,a,loc.reference);
  633. LOC_REGISTER,LOC_CREGISTER:
  634. a_load_const_reg(list,loc.size,a,loc.register);
  635. else
  636. internalerror(200203272);
  637. end;
  638. end;
  639. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  640. begin
  641. case loc.loc of
  642. LOC_REFERENCE,LOC_CREFERENCE:
  643. a_load_reg_ref(list,size,reg,loc.reference);
  644. LOC_REGISTER,LOC_CREGISTER:
  645. a_load_reg_reg(list,size,loc.size,reg,loc.register);
  646. else
  647. internalerror(200203271);
  648. end;
  649. end;
  650. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  651. begin
  652. case loc.loc of
  653. LOC_REFERENCE,LOC_CREFERENCE:
  654. a_load_ref_reg(list,loc.size,loc.reference,reg);
  655. LOC_REGISTER,LOC_CREGISTER:
  656. a_load_reg_reg(list,loc.size,loc.size,loc.register,reg);
  657. LOC_CONSTANT:
  658. a_load_const_reg(list,loc.size,loc.value,reg);
  659. else
  660. internalerror(200109092);
  661. end;
  662. end;
  663. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  664. begin
  665. case loc.loc of
  666. LOC_REFERENCE,LOC_CREFERENCE:
  667. a_load_ref_ref(list,loc.size,loc.reference,ref);
  668. LOC_REGISTER,LOC_CREGISTER:
  669. a_load_reg_ref(list,loc.size,loc.register,ref);
  670. LOC_CONSTANT:
  671. a_load_const_ref(list,loc.size,loc.value,ref);
  672. else
  673. internalerror(200109302);
  674. end;
  675. end;
  676. procedure tcg.a_call_loc(list : taasmoutput;const loc:tlocation);
  677. begin
  678. case loc.loc of
  679. LOC_REGISTER,LOC_CREGISTER:
  680. cg.a_call_reg(list,loc.register);
  681. LOC_REFERENCE,LOC_CREFERENCE :
  682. cg.a_call_ref(list,loc.reference);
  683. else
  684. internalerror(200203311);
  685. end;
  686. end;
  687. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg:tregister): boolean;
  688. var
  689. powerval : longint;
  690. begin
  691. optimize_op_const_reg := false;
  692. case op of
  693. { or with zero returns same result }
  694. OP_OR : if a = 0 then optimize_op_const_reg := true;
  695. { and with max returns same result }
  696. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  697. { division by 1 returns result }
  698. OP_DIV :
  699. begin
  700. if a = 1 then
  701. optimize_op_const_reg := true
  702. else if ispowerof2(int64(a), powerval) then
  703. begin
  704. a := powerval;
  705. op:= OP_SHR;
  706. end;
  707. exit;
  708. end;
  709. OP_IDIV:
  710. begin
  711. if a = 1 then
  712. optimize_op_const_reg := true
  713. else if ispowerof2(int64(a), powerval) then
  714. begin
  715. a := powerval;
  716. op:= OP_SAR;
  717. end;
  718. exit;
  719. end;
  720. OP_MUL,OP_IMUL:
  721. begin
  722. if a = 1 then
  723. optimize_op_const_reg := true
  724. else if ispowerof2(int64(a), powerval) then
  725. begin
  726. a := powerval;
  727. op:= OP_SHL;
  728. end;
  729. exit;
  730. end;
  731. OP_SAR,OP_SHL,OP_SHR:
  732. begin
  733. if a = 0 then
  734. optimize_op_const_reg := true;
  735. exit;
  736. end;
  737. end;
  738. end;
  739. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  740. begin
  741. case loc.loc of
  742. LOC_REFERENCE, LOC_CREFERENCE:
  743. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  744. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  745. a_loadfpu_reg_reg(list,loc.register,reg);
  746. else
  747. internalerror(200203301);
  748. end;
  749. end;
  750. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  751. begin
  752. case loc.loc of
  753. LOC_REFERENCE, LOC_CREFERENCE:
  754. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  755. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  756. a_loadfpu_reg_reg(list,reg,loc.register);
  757. else
  758. internalerror(48991);
  759. end;
  760. end;
  761. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);
  762. var
  763. ref : treference;
  764. t : Tregister;
  765. begin
  766. case locpara.loc of
  767. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  768. a_loadfpu_reg_reg(list,r,locpara.register);
  769. LOC_REFERENCE,LOC_CREFERENCE:
  770. begin
  771. if locpara.sp_fixup<>0 then
  772. begin
  773. t.enum:=stack_pointer_reg;
  774. a_op_const_reg(list,OP_ADD,locpara.sp_fixup,t);
  775. end;
  776. reference_reset(ref);
  777. ref.base:=locpara.reference.index;
  778. ref.offset:=locpara.reference.offset;
  779. a_loadfpu_reg_ref(list,size,r,ref);
  780. end
  781. else
  782. internalerror(2002071004);
  783. end;
  784. end;
  785. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  786. var
  787. tmpreg: tregister;
  788. begin
  789. tmpreg := get_scratch_reg_int(list);
  790. a_load_ref_reg(list,size,ref,tmpreg);
  791. a_op_const_reg(list,op,a,tmpreg);
  792. a_load_reg_ref(list,size,tmpreg,ref);
  793. free_scratch_reg(list,tmpreg);
  794. end;
  795. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  796. begin
  797. case loc.loc of
  798. LOC_REGISTER, LOC_CREGISTER:
  799. a_op_const_reg(list,op,a,loc.register);
  800. LOC_REFERENCE, LOC_CREFERENCE:
  801. a_op_const_ref(list,op,loc.size,a,loc.reference);
  802. else
  803. internalerror(200109061);
  804. end;
  805. end;
  806. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  807. var
  808. tmpreg: tregister;
  809. begin
  810. tmpreg := get_scratch_reg_int(list);
  811. a_load_ref_reg(list,size,ref,tmpreg);
  812. a_op_reg_reg(list,op,size,reg,tmpreg);
  813. a_load_reg_ref(list,size,tmpreg,ref);
  814. free_scratch_reg(list,tmpreg);
  815. end;
  816. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  817. var
  818. tmpreg: tregister;
  819. begin
  820. case op of
  821. OP_NOT,OP_NEG:
  822. { handle it as "load ref,reg; op reg" }
  823. begin
  824. a_load_ref_reg(list,size,ref,reg);
  825. a_op_reg_reg(list,op,size,reg,reg);
  826. end;
  827. else
  828. begin
  829. tmpreg := get_scratch_reg_int(list);
  830. a_load_ref_reg(list,size,ref,tmpreg);
  831. a_op_reg_reg(list,op,size,tmpreg,reg);
  832. free_scratch_reg(list,tmpreg);
  833. end;
  834. end;
  835. end;
  836. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  837. begin
  838. case loc.loc of
  839. LOC_REGISTER, LOC_CREGISTER:
  840. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  841. LOC_REFERENCE, LOC_CREFERENCE:
  842. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  843. else
  844. internalerror(200109061);
  845. end;
  846. end;
  847. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  848. var
  849. tmpreg: tregister;
  850. begin
  851. case loc.loc of
  852. LOC_REGISTER,LOC_CREGISTER:
  853. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  854. LOC_REFERENCE,LOC_CREFERENCE:
  855. begin
  856. tmpreg := get_scratch_reg_int(list);
  857. tmpreg:=rg.makeregsize(tmpreg,loc.size);
  858. a_load_ref_reg(list,loc.size,ref,tmpreg);
  859. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  860. free_scratch_reg(list,tmpreg);
  861. end;
  862. else
  863. internalerror(200109061);
  864. end;
  865. end;
  866. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  867. size: tcgsize; a: aword; src, dst: tregister);
  868. begin
  869. a_load_reg_reg(list,size,size,src,dst);
  870. a_op_const_reg(list,op,a,dst);
  871. end;
  872. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  873. size: tcgsize; src1, src2, dst: tregister);
  874. begin
  875. a_load_reg_reg(list,size,size,src2,dst);
  876. a_op_reg_reg(list,op,size,src1,dst);
  877. end;
  878. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  879. l : tasmlabel);
  880. var
  881. tmpreg: tregister;
  882. begin
  883. tmpreg := get_scratch_reg_int(list);
  884. a_load_ref_reg(list,size,ref,tmpreg);
  885. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  886. free_scratch_reg(list,tmpreg);
  887. end;
  888. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  889. l : tasmlabel);
  890. begin
  891. case loc.loc of
  892. LOC_REGISTER,LOC_CREGISTER:
  893. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  894. LOC_REFERENCE,LOC_CREFERENCE:
  895. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  896. else
  897. internalerror(200109061);
  898. end;
  899. end;
  900. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  901. var
  902. tmpreg: tregister;
  903. begin
  904. tmpreg := get_scratch_reg_int(list);
  905. a_load_ref_reg(list,size,ref,tmpreg);
  906. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  907. free_scratch_reg(list,tmpreg);
  908. end;
  909. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  910. begin
  911. case loc.loc of
  912. LOC_REGISTER,
  913. LOC_CREGISTER:
  914. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  915. LOC_REFERENCE,
  916. LOC_CREFERENCE :
  917. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  918. LOC_CONSTANT:
  919. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  920. else
  921. internalerror(200203231);
  922. end;
  923. end;
  924. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  925. l : tasmlabel);
  926. var
  927. tmpreg: tregister;
  928. begin
  929. case loc.loc of
  930. LOC_REGISTER,LOC_CREGISTER:
  931. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  932. LOC_REFERENCE,LOC_CREFERENCE:
  933. begin
  934. {$ifdef i386}
  935. { the following is done with defines to avoid a speed penalty, }
  936. { since all this is only necessary for the 80x86 (because EDI }
  937. { doesn't have an 8bit component which is directly addressable) }
  938. if size in [OS_8,OS_S8] then
  939. tmpreg := rg.getregisterint(list)
  940. else
  941. {$endif i386}
  942. tmpreg := get_scratch_reg_int(list);
  943. tmpreg := rg.makeregsize(tmpreg,size);
  944. a_load_ref_reg(list,size,loc.reference,tmpreg);
  945. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  946. {$ifdef i386}
  947. if size in [OS_8,OS_S8] then
  948. rg.ungetregister(list,tmpreg)
  949. else
  950. {$endif i386}
  951. free_scratch_reg(list,tmpreg);
  952. end
  953. else
  954. internalerror(200109061);
  955. end;
  956. end;
  957. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  958. begin
  959. reg_cgsize := OS_INT;
  960. end;
  961. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  962. begin
  963. {$ifdef FPC}
  964. {$warning FIX ME!}
  965. {$endif}
  966. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  967. if loadref then
  968. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  969. else
  970. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  971. if delsource then
  972. reference_release(list,source);
  973. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  974. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  975. g_maybe_loadself(list);
  976. end;
  977. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  978. var
  979. href : treference;
  980. incrfunc : string;
  981. begin
  982. { These functions should not change the registers (they use
  983. the saveregister proc directive }
  984. if is_interfacecom(t) then
  985. incrfunc:='FPC_INTF_INCR_REF'
  986. else if is_ansistring(t) then
  987. incrfunc:='FPC_ANSISTR_INCR_REF'
  988. else if is_widestring(t) then
  989. incrfunc:='FPC_WIDESTR_INCR_REF'
  990. else if is_dynamic_array(t) then
  991. incrfunc:='FPC_DYNARRAY_INCR_REF'
  992. else
  993. incrfunc:='';
  994. { call the special incr function or the generic addref }
  995. if incrfunc<>'' then
  996. begin
  997. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  998. a_call_name(list,incrfunc);
  999. end
  1000. else
  1001. begin
  1002. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1003. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1004. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1005. a_call_name(list,'FPC_ADDREF');
  1006. end;
  1007. end;
  1008. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1009. var
  1010. href : treference;
  1011. decrfunc : string;
  1012. needrtti : boolean;
  1013. begin
  1014. needrtti:=false;
  1015. if is_interfacecom(t) then
  1016. decrfunc:='FPC_INTF_DECR_REF'
  1017. else if is_ansistring(t) then
  1018. decrfunc:='FPC_ANSISTR_DECR_REF'
  1019. else if is_widestring(t) then
  1020. decrfunc:='FPC_WIDESTR_DECR_REF'
  1021. else if is_dynamic_array(t) then
  1022. begin
  1023. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1024. needrtti:=true;
  1025. end
  1026. else
  1027. decrfunc:='';
  1028. { call the special decr function or the generic decref }
  1029. if decrfunc<>'' then
  1030. begin
  1031. if needrtti then
  1032. begin
  1033. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1034. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1035. end;
  1036. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1037. a_call_name(list,decrfunc);
  1038. end
  1039. else
  1040. begin
  1041. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1042. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1043. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1044. a_call_name(list,'FPC_DECREF');
  1045. end;
  1046. end;
  1047. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1048. var
  1049. href : treference;
  1050. begin
  1051. if is_ansistring(t) or
  1052. is_widestring(t) or
  1053. is_interfacecom(t) then
  1054. a_load_const_ref(list,OS_ADDR,0,ref)
  1055. else
  1056. begin
  1057. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1058. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1059. if loadref then
  1060. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1061. else
  1062. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1063. a_call_name(list,'FPC_INITIALIZE');
  1064. end;
  1065. end;
  1066. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1067. var
  1068. href : treference;
  1069. begin
  1070. if is_ansistring(t) or
  1071. is_widestring(t) or
  1072. is_interfacecom(t) then
  1073. g_decrrefcount(list,t,ref)
  1074. else
  1075. begin
  1076. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1077. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1078. if loadref then
  1079. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1080. else
  1081. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1082. a_call_name(list,'FPC_FINALIZE');
  1083. end;
  1084. end;
  1085. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  1086. { generate range checking code for the value at location p. The type }
  1087. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1088. { is the original type used at that location. When both defs are equal }
  1089. { the check is also insert (needed for succ,pref,inc,dec) }
  1090. const
  1091. {$ifdef ver1_0}
  1092. awordsignedmax=high(longint);
  1093. {$else}
  1094. awordsignedmax=high(aword) div 2;
  1095. {$endif}
  1096. var
  1097. neglabel : tasmlabel;
  1098. hreg : tregister;
  1099. fromdef : tdef;
  1100. lto,hto,
  1101. lfrom,hfrom : TConstExprInt;
  1102. from_signed: boolean;
  1103. begin
  1104. { range checking on and range checkable value? }
  1105. if not(cs_check_range in aktlocalswitches) or
  1106. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1107. exit;
  1108. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  1109. begin
  1110. cg64.g_rangecheck64(list,p,todef);
  1111. exit;
  1112. end;
  1113. { only check when assigning to scalar, subranges are different, }
  1114. { when todef=fromdef then the check is always generated }
  1115. fromdef:=p.resulttype.def;
  1116. getrange(p.resulttype.def,lfrom,hfrom);
  1117. getrange(todef,lto,hto);
  1118. { no range check if from and to are equal and are both longint/dword }
  1119. { (if we have a 32bit processor) or int64/qword, since such }
  1120. { operations can at most cause overflows (JM) }
  1121. { Note that these checks are mostly processor independent, they only }
  1122. { have to be changed once we introduce 64bit subrange types }
  1123. if (fromdef = todef) and
  1124. (fromdef.deftype=orddef) and
  1125. (((sizeof(aword) = 4) and
  1126. (((torddef(fromdef).typ = s32bit) and
  1127. (lfrom = low(longint)) and
  1128. (hfrom = high(longint))) or
  1129. ((torddef(fromdef).typ = u32bit) and
  1130. (lfrom = low(cardinal)) and
  1131. (hfrom = high(cardinal)))))) then
  1132. exit;
  1133. if todef<>fromdef then
  1134. begin
  1135. { if the from-range falls completely in the to-range, no check }
  1136. { is necessary }
  1137. if (lto<=lfrom) and (hto>=hfrom) then
  1138. exit;
  1139. end;
  1140. { generate the rangecheck code for the def where we are going to }
  1141. { store the result }
  1142. { use the trick that }
  1143. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1144. { To be able to do that, we have to make sure however that either }
  1145. { fromdef and todef are both signed or unsigned, or that we leave }
  1146. { the parts < 0 and > maxlongint out }
  1147. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1148. from_signed := is_signed(fromdef);
  1149. if from_signed xor is_signed(todef) then
  1150. if from_signed then
  1151. { from is signed, to is unsigned }
  1152. begin
  1153. { if high(from) < 0 -> always range error }
  1154. if (hfrom < 0) or
  1155. { if low(to) > maxlongint also range error }
  1156. (lto > awordsignedmax) then
  1157. begin
  1158. a_call_name(list,'FPC_RANGEERROR');
  1159. exit
  1160. end;
  1161. { from is signed and to is unsigned -> when looking at from }
  1162. { as an unsigned value, it must be < maxlongint (otherwise }
  1163. { it's negative, which is invalid since "to" is unsigned) }
  1164. if hto > awordsignedmax then
  1165. hto := awordsignedmax;
  1166. end
  1167. else
  1168. { from is unsigned, to is signed }
  1169. begin
  1170. if (lfrom > awordsignedmax) or
  1171. (hto < 0) then
  1172. begin
  1173. a_call_name(list,'FPC_RANGEERROR');
  1174. exit
  1175. end;
  1176. { from is unsigned and to is signed -> when looking at to }
  1177. { as an unsigned value, it must be >= 0 (since negative }
  1178. { values are the same as values > maxlongint) }
  1179. if lto < 0 then
  1180. lto := 0;
  1181. end;
  1182. hreg := get_scratch_reg_int(list);
  1183. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1184. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1185. aword(lto),p.location.register,hreg)
  1186. else
  1187. begin
  1188. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1189. p.location.reference,hreg);
  1190. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  1191. end;
  1192. objectlibrary.getlabel(neglabel);
  1193. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1194. { !!! should happen right after the compare (JM) }
  1195. free_scratch_reg(list,hreg);
  1196. a_call_name(list,'FPC_RANGEERROR');
  1197. a_label(list,neglabel);
  1198. end;
  1199. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1200. begin
  1201. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  1202. a_call_name(list,'FPC_STACKCHECK');
  1203. end;
  1204. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1205. var
  1206. tmpreg : tregister;
  1207. begin
  1208. tmpreg := get_scratch_reg_int(list);
  1209. g_flags2reg(list,size,f,tmpreg);
  1210. a_load_reg_ref(list,size,tmpreg,ref);
  1211. free_scratch_reg(list,tmpreg);
  1212. end;
  1213. procedure tcg.g_maybe_loadself(list : taasmoutput);
  1214. var
  1215. hp : treference;
  1216. p : tprocinfo;
  1217. i : longint;
  1218. spr : Tregister;
  1219. begin
  1220. spr.enum:=SELF_POINTER_REG;
  1221. if assigned(procinfo._class) then
  1222. begin
  1223. list.concat(tai_regalloc.Alloc(spr));
  1224. if lexlevel>normal_function_level then
  1225. begin
  1226. reference_reset_base(hp,procinfo.framepointer,procinfo.framepointer_offset);
  1227. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1228. p:=procinfo.parent;
  1229. for i:=3 to lexlevel-1 do
  1230. begin
  1231. reference_reset_base(hp,spr,p.framepointer_offset);
  1232. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1233. p:=p.parent;
  1234. end;
  1235. reference_reset_base(hp,spr,p.selfpointer_offset);
  1236. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1237. end
  1238. else
  1239. begin
  1240. reference_reset_base(hp,procinfo.framepointer,procinfo.selfpointer_offset);
  1241. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1242. end;
  1243. end;
  1244. end;
  1245. procedure tcg.g_maybe_testself(list : taasmoutput);
  1246. var
  1247. OKLabel : tasmlabel;
  1248. dummyloc : tparalocation;
  1249. spr : Tregister;
  1250. begin
  1251. if (cs_check_object in aktlocalswitches) or
  1252. (cs_check_range in aktlocalswitches) then
  1253. begin
  1254. spr.enum:=SELF_POINTER_REG;
  1255. objectlibrary.getlabel(oklabel);
  1256. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,spr,oklabel);
  1257. a_param_const(list,OS_INT,210,dummyloc);
  1258. a_call_name(list,'FPC_HANDLEERROR');
  1259. a_label(list,oklabel);
  1260. end;
  1261. end;
  1262. {*****************************************************************************
  1263. Entry/Exit Code Functions
  1264. *****************************************************************************}
  1265. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1266. var
  1267. href : treference;
  1268. hregister : tregister;
  1269. spr,acc : Tregister;
  1270. begin
  1271. acc.enum:=accumulator;
  1272. spr.enum:=SELF_POINTER_REG;
  1273. if is_class(procinfo._class) then
  1274. begin
  1275. if (cs_implicit_exceptions in aktmoduleswitches) then
  1276. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1277. { parameter 2 : self pointer / flag }
  1278. {!! this is a terrible hack, normally the helper should get three params : }
  1279. { one with self register, one with flag and one with VMT pointer }
  1280. {reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);}
  1281. a_param_reg(list, OS_ADDR, spr, paramanager.getintparaloc(2));
  1282. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1283. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1284. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1285. a_call_name(list,'FPC_NEW_CLASS');
  1286. a_load_reg_reg(list,OS_ADDR,OS_ADDR,acc,spr);
  1287. { save the self pointer result }
  1288. a_load_reg_ref(list,OS_ADDR,spr,href);
  1289. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1290. end
  1291. else if is_object(procinfo._class) then
  1292. begin
  1293. { parameter 3 :vmt_offset }
  1294. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1295. { parameter 2 : address of pointer to vmt }
  1296. { this is the first(?) parameter which was pushed to the constructor }
  1297. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1298. hregister:=get_scratch_reg_address(list);
  1299. a_loadaddr_ref_reg(list, href, hregister);
  1300. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1301. free_scratch_reg(list, hregister);
  1302. { parameter 1 : address of self pointer }
  1303. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1304. hregister:=get_scratch_reg_address(list);
  1305. a_loadaddr_ref_reg(list, href, hregister);
  1306. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1307. free_scratch_reg(list, hregister);
  1308. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1309. a_load_reg_reg(list,OS_ADDR,OS_ADDR,acc,spr);
  1310. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1311. end
  1312. else
  1313. internalerror(200006161);
  1314. end;
  1315. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1316. var
  1317. nofinal : tasmlabel;
  1318. href : treference;
  1319. hregister : tregister;
  1320. spr : Tregister;
  1321. begin
  1322. spr.enum:=SELF_POINTER_REG;
  1323. if is_class(procinfo._class) then
  1324. begin
  1325. { 2nd parameter : flag }
  1326. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);
  1327. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1328. { 1st parameter to destructor : self }
  1329. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1330. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1331. a_call_name(list,'FPC_DISPOSE_CLASS')
  1332. end
  1333. else if is_object(procinfo._class) then
  1334. begin
  1335. { must the object be finalized ? }
  1336. if procinfo._class.needs_inittable then
  1337. begin
  1338. objectlibrary.getlabel(nofinal);
  1339. reference_reset_base(href,procinfo.framepointer,target_info.first_parm_offset);
  1340. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1341. reference_reset_base(href,spr,0);
  1342. g_finalize(list,procinfo._class,href,false);
  1343. a_label(list,nofinal);
  1344. end;
  1345. { actually call destructor }
  1346. { parameter 3 :vmt_offset }
  1347. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1348. { parameter 2 : pointer to vmt }
  1349. { this is the first parameter which was pushed to the destructor }
  1350. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1351. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1352. { parameter 1 : address of self pointer }
  1353. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1354. hregister:=get_scratch_reg_address(list);
  1355. a_loadaddr_ref_reg(list, href, hregister);
  1356. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1357. free_scratch_reg(list, hregister);
  1358. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1359. end
  1360. else
  1361. internalerror(200006162);
  1362. end;
  1363. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1364. var
  1365. href : treference;
  1366. hregister : tregister;
  1367. spr : Tregister;
  1368. begin
  1369. spr.enum:=SELF_POINTER_REG;
  1370. if is_class(procinfo._class) then
  1371. begin
  1372. {
  1373. Dispose of the class then set self_pointer to nil
  1374. both in stack and in self register.
  1375. }
  1376. { 2nd parameter : flag }
  1377. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1378. { 1st parameter to destructor : self }
  1379. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1380. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1381. a_call_name(list,'FPC_DISPOSE_CLASS');
  1382. { SET SELF TO NIL }
  1383. a_load_const_reg(list,OS_ADDR,0,spr);
  1384. { set the self pointer in the stack to nil }
  1385. a_load_reg_ref(list,OS_ADDR,spr,href);
  1386. end
  1387. else if is_object(procinfo._class) then
  1388. begin
  1389. { parameter 3 :vmt_offset }
  1390. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1391. { parameter 2 : address of pointer to vmt }
  1392. { this is the first(?) parameter which was pushed to the constructor }
  1393. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1394. hregister:=get_scratch_reg_address(list);
  1395. a_loadaddr_ref_reg(list, href, hregister);
  1396. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1397. free_scratch_reg(list, hregister);
  1398. { parameter 1 : address of self pointer }
  1399. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1400. hregister:=get_scratch_reg_address(list);
  1401. a_loadaddr_ref_reg(list, href, hregister);
  1402. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1403. free_scratch_reg(list, hregister);
  1404. a_call_name(list,'FPC_HELP_FAIL');
  1405. { SET SELF TO NIL }
  1406. a_load_const_reg(list,OS_ADDR,0,spr);
  1407. end
  1408. else
  1409. internalerror(200006163);
  1410. end;
  1411. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1412. begin
  1413. end;
  1414. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1415. begin
  1416. end;
  1417. procedure tcg.g_profilecode(list : taasmoutput);
  1418. begin
  1419. end;
  1420. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1421. var r:Tregister;
  1422. begin
  1423. r.enum:=accumulator;
  1424. a_load_reg_ref(list, OS_S32, r, href);
  1425. end;
  1426. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1427. begin
  1428. a_load_const_ref(list, OS_S32, a, href);
  1429. end;
  1430. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1431. var r:Tregister;
  1432. begin
  1433. r.enum:=accumulator;
  1434. a_load_ref_reg(list, OS_S32, href, r);
  1435. end;
  1436. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;
  1437. regsrc,regdst : tregister64);
  1438. begin
  1439. a_load64_reg_reg(list,regsrc,regdst);
  1440. a_op64_const_reg(list,op,value,regdst);
  1441. end;
  1442. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1443. begin
  1444. a_load64_reg_reg(list,regsrc2,regdst);
  1445. a_op64_reg_reg(list,op,regsrc1,regdst);
  1446. end;
  1447. initialization
  1448. ;
  1449. finalization
  1450. cg.free;
  1451. cg64.free;
  1452. end.
  1453. {
  1454. $Log$
  1455. Revision 1.72 2003-01-09 22:00:53 florian
  1456. * fixed some PowerPC issues
  1457. Revision 1.71 2003/01/09 20:41:10 florian
  1458. * fixed broken PowerPC compiler
  1459. Revision 1.70 2003/01/08 18:43:56 daniel
  1460. * Tregister changed into a record
  1461. Revision 1.69 2002/12/24 15:56:50 peter
  1462. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1463. this for the pageprotection
  1464. Revision 1.68 2002/12/20 18:14:04 peter
  1465. * removed some runerror and writeln
  1466. Revision 1.67 2002/12/14 15:02:03 carl
  1467. * maxoperands -> max_operands (for portability in rautils.pas)
  1468. * fix some range-check errors with loadconst
  1469. + add ncgadd unit to m68k
  1470. * some bugfix of a_param_reg with LOC_CREFERENCE
  1471. Revision 1.66 2002/11/25 17:43:16 peter
  1472. * splitted defbase in defutil,symutil,defcmp
  1473. * merged isconvertable and is_equal into compare_defs(_ext)
  1474. * made operator search faster by walking the list only once
  1475. Revision 1.65 2002/11/17 16:27:31 carl
  1476. * document flags2reg
  1477. Revision 1.64 2002/11/16 17:06:28 peter
  1478. * return error 210 for failed self test
  1479. Revision 1.63 2002/11/15 01:58:46 peter
  1480. * merged changes from 1.0.7 up to 04-11
  1481. - -V option for generating bug report tracing
  1482. - more tracing for option parsing
  1483. - errors for cdecl and high()
  1484. - win32 import stabs
  1485. - win32 records<=8 are returned in eax:edx (turned off by default)
  1486. - heaptrc update
  1487. - more info for temp management in .s file with EXTDEBUG
  1488. Revision 1.62 2002/10/16 19:01:43 peter
  1489. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1490. implicit exception frames for procedures with initialized variables
  1491. and for constructors. The default is on for compatibility
  1492. Revision 1.61 2002/10/05 12:43:23 carl
  1493. * fixes for Delphi 6 compilation
  1494. (warning : Some features do not work under Delphi)
  1495. Revision 1.60 2002/10/02 18:20:52 peter
  1496. * Copy() is now internal syssym that calls compilerprocs
  1497. Revision 1.59 2002/09/17 18:54:02 jonas
  1498. * a_load_reg_reg() now has two size parameters: source and dest. This
  1499. allows some optimizations on architectures that don't encode the
  1500. register size in the register name.
  1501. Revision 1.58 2002/09/09 19:29:29 peter
  1502. * fixed dynarr_decr_ref call
  1503. Revision 1.57 2002/09/07 15:25:01 peter
  1504. * old logs removed and tabs fixed
  1505. Revision 1.56 2002/09/01 21:04:47 florian
  1506. * several powerpc related stuff fixed
  1507. Revision 1.55 2002/09/01 17:05:43 florian
  1508. + added abstract tcg.g_removevaluepara_openarray
  1509. Revision 1.54 2002/09/01 12:09:27 peter
  1510. + a_call_reg, a_call_loc added
  1511. * removed exprasmlist references
  1512. Revision 1.53 2002/08/19 18:17:48 carl
  1513. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  1514. * more fixes to m68k for 64-bit operations
  1515. Revision 1.52 2002/08/17 22:09:43 florian
  1516. * result type handling in tcgcal.pass_2 overhauled
  1517. * better tnode.dowrite
  1518. * some ppc stuff fixed
  1519. Revision 1.51 2002/08/17 09:23:33 florian
  1520. * first part of procinfo rewrite
  1521. Revision 1.50 2002/08/16 14:24:57 carl
  1522. * issameref() to test if two references are the same (then emit no opcodes)
  1523. + ret_in_reg to replace ret_in_acc
  1524. (fix some register allocation bugs at the same time)
  1525. + save_std_register now has an extra parameter which is the
  1526. usedinproc registers
  1527. Revision 1.49 2002/08/15 08:13:54 carl
  1528. - a_load_sym_ofs_reg removed
  1529. * loadvmt now calls loadaddr_ref_reg instead
  1530. Revision 1.48 2002/08/14 19:26:02 carl
  1531. + routine to optimize opcodes with constants
  1532. Revision 1.47 2002/08/11 14:32:26 peter
  1533. * renamed current_library to objectlibrary
  1534. Revision 1.46 2002/08/11 13:24:11 peter
  1535. * saving of asmsymbols in ppu supported
  1536. * asmsymbollist global is removed and moved into a new class
  1537. tasmlibrarydata that will hold the info of a .a file which
  1538. corresponds with a single module. Added librarydata to tmodule
  1539. to keep the library info stored for the module. In the future the
  1540. objectfiles will also be stored to the tasmlibrarydata class
  1541. * all getlabel/newasmsymbol and friends are moved to the new class
  1542. Revision 1.45 2002/08/10 17:15:20 jonas
  1543. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  1544. Revision 1.44 2002/08/09 19:10:05 carl
  1545. - moved new_exception and free_exception to ncgutils
  1546. Revision 1.43 2002/08/05 18:27:48 carl
  1547. + more more more documentation
  1548. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1549. Revision 1.42 2002/08/04 19:08:21 carl
  1550. + added generic exception support (still does not work!)
  1551. + more documentation
  1552. Revision 1.41 2002/07/30 20:50:43 florian
  1553. * the code generator knows now if parameters are in registers
  1554. Revision 1.40 2002/07/29 21:16:02 florian
  1555. * some more ppc fixes
  1556. Revision 1.39 2002/07/28 15:56:00 jonas
  1557. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1558. generic implementation
  1559. Revision 1.38 2002/07/27 19:53:51 jonas
  1560. + generic implementation of tcg.g_flags2ref()
  1561. * tcg.flags2xxx() now also needs a size parameter
  1562. Revision 1.37 2002/07/20 11:57:53 florian
  1563. * types.pas renamed to defbase.pas because D6 contains a types
  1564. unit so this would conflicts if D6 programms are compiled
  1565. + Willamette/SSE2 instructions to assembler added
  1566. }