cgobj.pas 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  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. var r:Tregister;
  652. begin
  653. {$ifdef i386}
  654. {For safety convert location register to enum for now...}
  655. convert_register_to_enum(reg);
  656. {$endif}
  657. case loc.loc of
  658. LOC_REFERENCE,LOC_CREFERENCE:
  659. a_load_ref_reg(list,loc.size,loc.reference,reg);
  660. LOC_REGISTER,LOC_CREGISTER:
  661. begin
  662. {For safety convert location register to enum for now...}
  663. r:=loc.register;
  664. convert_register_to_enum(r);
  665. a_load_reg_reg(list,loc.size,loc.size,r,reg);
  666. end;
  667. LOC_CONSTANT:
  668. a_load_const_reg(list,loc.size,loc.value,reg);
  669. else
  670. internalerror(200109092);
  671. end;
  672. end;
  673. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  674. begin
  675. case loc.loc of
  676. LOC_REFERENCE,LOC_CREFERENCE:
  677. a_load_ref_ref(list,loc.size,loc.reference,ref);
  678. LOC_REGISTER,LOC_CREGISTER:
  679. a_load_reg_ref(list,loc.size,loc.register,ref);
  680. LOC_CONSTANT:
  681. a_load_const_ref(list,loc.size,loc.value,ref);
  682. else
  683. internalerror(200109302);
  684. end;
  685. end;
  686. procedure tcg.a_call_loc(list : taasmoutput;const loc:tlocation);
  687. begin
  688. case loc.loc of
  689. LOC_REGISTER,LOC_CREGISTER:
  690. cg.a_call_reg(list,loc.register);
  691. LOC_REFERENCE,LOC_CREFERENCE :
  692. cg.a_call_ref(list,loc.reference);
  693. else
  694. internalerror(200203311);
  695. end;
  696. end;
  697. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aword; var reg:tregister): boolean;
  698. var
  699. powerval : longint;
  700. begin
  701. optimize_op_const_reg := false;
  702. case op of
  703. { or with zero returns same result }
  704. OP_OR : if a = 0 then optimize_op_const_reg := true;
  705. { and with max returns same result }
  706. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  707. { division by 1 returns result }
  708. OP_DIV :
  709. begin
  710. if a = 1 then
  711. optimize_op_const_reg := true
  712. else if ispowerof2(int64(a), powerval) then
  713. begin
  714. a := powerval;
  715. op:= OP_SHR;
  716. end;
  717. exit;
  718. end;
  719. OP_IDIV:
  720. begin
  721. if a = 1 then
  722. optimize_op_const_reg := true
  723. else if ispowerof2(int64(a), powerval) then
  724. begin
  725. a := powerval;
  726. op:= OP_SAR;
  727. end;
  728. exit;
  729. end;
  730. OP_MUL,OP_IMUL:
  731. begin
  732. if a = 1 then
  733. optimize_op_const_reg := true
  734. else if ispowerof2(int64(a), powerval) then
  735. begin
  736. a := powerval;
  737. op:= OP_SHL;
  738. end;
  739. exit;
  740. end;
  741. OP_SAR,OP_SHL,OP_SHR:
  742. begin
  743. if a = 0 then
  744. optimize_op_const_reg := true;
  745. exit;
  746. end;
  747. end;
  748. end;
  749. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  750. begin
  751. case loc.loc of
  752. LOC_REFERENCE, LOC_CREFERENCE:
  753. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  754. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  755. a_loadfpu_reg_reg(list,loc.register,reg);
  756. else
  757. internalerror(200203301);
  758. end;
  759. end;
  760. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  761. begin
  762. case loc.loc of
  763. LOC_REFERENCE, LOC_CREFERENCE:
  764. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  765. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  766. a_loadfpu_reg_reg(list,reg,loc.register);
  767. else
  768. internalerror(48991);
  769. end;
  770. end;
  771. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);
  772. var
  773. ref : treference;
  774. t : Tregister;
  775. begin
  776. case locpara.loc of
  777. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  778. a_loadfpu_reg_reg(list,r,locpara.register);
  779. LOC_REFERENCE,LOC_CREFERENCE:
  780. begin
  781. if locpara.sp_fixup<>0 then
  782. begin
  783. t.enum:=stack_pointer_reg;
  784. a_op_const_reg(list,OP_ADD,locpara.sp_fixup,t);
  785. end;
  786. reference_reset(ref);
  787. ref.base:=locpara.reference.index;
  788. ref.offset:=locpara.reference.offset;
  789. a_loadfpu_reg_ref(list,size,r,ref);
  790. end
  791. else
  792. internalerror(2002071004);
  793. end;
  794. end;
  795. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  796. var
  797. tmpreg: tregister;
  798. begin
  799. tmpreg := get_scratch_reg_int(list);
  800. a_load_ref_reg(list,size,ref,tmpreg);
  801. a_op_const_reg(list,op,a,tmpreg);
  802. a_load_reg_ref(list,size,tmpreg,ref);
  803. free_scratch_reg(list,tmpreg);
  804. end;
  805. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  806. begin
  807. case loc.loc of
  808. LOC_REGISTER, LOC_CREGISTER:
  809. a_op_const_reg(list,op,a,loc.register);
  810. LOC_REFERENCE, LOC_CREFERENCE:
  811. a_op_const_ref(list,op,loc.size,a,loc.reference);
  812. else
  813. internalerror(200109061);
  814. end;
  815. end;
  816. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  817. var
  818. tmpreg: tregister;
  819. begin
  820. tmpreg := get_scratch_reg_int(list);
  821. a_load_ref_reg(list,size,ref,tmpreg);
  822. a_op_reg_reg(list,op,size,reg,tmpreg);
  823. a_load_reg_ref(list,size,tmpreg,ref);
  824. free_scratch_reg(list,tmpreg);
  825. end;
  826. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  827. var
  828. tmpreg: tregister;
  829. begin
  830. case op of
  831. OP_NOT,OP_NEG:
  832. { handle it as "load ref,reg; op reg" }
  833. begin
  834. a_load_ref_reg(list,size,ref,reg);
  835. a_op_reg_reg(list,op,size,reg,reg);
  836. end;
  837. else
  838. begin
  839. tmpreg := get_scratch_reg_int(list);
  840. a_load_ref_reg(list,size,ref,tmpreg);
  841. a_op_reg_reg(list,op,size,tmpreg,reg);
  842. free_scratch_reg(list,tmpreg);
  843. end;
  844. end;
  845. end;
  846. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  847. begin
  848. case loc.loc of
  849. LOC_REGISTER, LOC_CREGISTER:
  850. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  851. LOC_REFERENCE, LOC_CREFERENCE:
  852. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  853. else
  854. internalerror(200109061);
  855. end;
  856. end;
  857. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  858. var
  859. tmpreg: tregister;
  860. begin
  861. case loc.loc of
  862. LOC_REGISTER,LOC_CREGISTER:
  863. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  864. LOC_REFERENCE,LOC_CREFERENCE:
  865. begin
  866. tmpreg := get_scratch_reg_int(list);
  867. tmpreg:=rg.makeregsize(tmpreg,loc.size);
  868. a_load_ref_reg(list,loc.size,ref,tmpreg);
  869. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  870. free_scratch_reg(list,tmpreg);
  871. end;
  872. else
  873. internalerror(200109061);
  874. end;
  875. end;
  876. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  877. size: tcgsize; a: aword; src, dst: tregister);
  878. begin
  879. a_load_reg_reg(list,size,size,src,dst);
  880. a_op_const_reg(list,op,a,dst);
  881. end;
  882. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  883. size: tcgsize; src1, src2, dst: tregister);
  884. begin
  885. a_load_reg_reg(list,size,size,src2,dst);
  886. a_op_reg_reg(list,op,size,src1,dst);
  887. end;
  888. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  889. l : tasmlabel);
  890. var
  891. tmpreg: tregister;
  892. begin
  893. tmpreg := get_scratch_reg_int(list);
  894. a_load_ref_reg(list,size,ref,tmpreg);
  895. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  896. free_scratch_reg(list,tmpreg);
  897. end;
  898. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  899. l : tasmlabel);
  900. begin
  901. case loc.loc of
  902. LOC_REGISTER,LOC_CREGISTER:
  903. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  904. LOC_REFERENCE,LOC_CREFERENCE:
  905. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  906. else
  907. internalerror(200109061);
  908. end;
  909. end;
  910. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  911. var
  912. tmpreg: tregister;
  913. begin
  914. tmpreg := get_scratch_reg_int(list);
  915. a_load_ref_reg(list,size,ref,tmpreg);
  916. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  917. free_scratch_reg(list,tmpreg);
  918. end;
  919. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  920. begin
  921. case loc.loc of
  922. LOC_REGISTER,
  923. LOC_CREGISTER:
  924. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  925. LOC_REFERENCE,
  926. LOC_CREFERENCE :
  927. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  928. LOC_CONSTANT:
  929. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  930. else
  931. internalerror(200203231);
  932. end;
  933. end;
  934. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  935. l : tasmlabel);
  936. var
  937. tmpreg: tregister;
  938. begin
  939. case loc.loc of
  940. LOC_REGISTER,LOC_CREGISTER:
  941. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  942. LOC_REFERENCE,LOC_CREFERENCE:
  943. begin
  944. {$ifdef i386}
  945. { the following is done with defines to avoid a speed penalty, }
  946. { since all this is only necessary for the 80x86 (because EDI }
  947. { doesn't have an 8bit component which is directly addressable) }
  948. if size in [OS_8,OS_S8] then
  949. tmpreg := rg.getregisterint(list)
  950. else
  951. {$endif i386}
  952. tmpreg := get_scratch_reg_int(list);
  953. tmpreg := rg.makeregsize(tmpreg,size);
  954. a_load_ref_reg(list,size,loc.reference,tmpreg);
  955. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  956. {$ifdef i386}
  957. if size in [OS_8,OS_S8] then
  958. rg.ungetregister(list,tmpreg)
  959. else
  960. {$endif i386}
  961. free_scratch_reg(list,tmpreg);
  962. end
  963. else
  964. internalerror(200109061);
  965. end;
  966. end;
  967. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  968. begin
  969. reg_cgsize := OS_INT;
  970. end;
  971. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  972. begin
  973. {$ifdef FPC}
  974. {$warning FIX ME!}
  975. {$endif}
  976. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  977. if loadref then
  978. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  979. else
  980. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  981. if delsource then
  982. reference_release(list,source);
  983. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  984. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  985. g_maybe_loadself(list);
  986. end;
  987. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  988. var
  989. href : treference;
  990. incrfunc : string;
  991. begin
  992. { These functions should not change the registers (they use
  993. the saveregister proc directive }
  994. if is_interfacecom(t) then
  995. incrfunc:='FPC_INTF_INCR_REF'
  996. else if is_ansistring(t) then
  997. incrfunc:='FPC_ANSISTR_INCR_REF'
  998. else if is_widestring(t) then
  999. incrfunc:='FPC_WIDESTR_INCR_REF'
  1000. else if is_dynamic_array(t) then
  1001. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1002. else
  1003. incrfunc:='';
  1004. { call the special incr function or the generic addref }
  1005. if incrfunc<>'' then
  1006. begin
  1007. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  1008. a_call_name(list,incrfunc);
  1009. end
  1010. else
  1011. begin
  1012. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1013. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1014. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1015. a_call_name(list,'FPC_ADDREF');
  1016. end;
  1017. end;
  1018. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1019. var
  1020. href : treference;
  1021. decrfunc : string;
  1022. needrtti : boolean;
  1023. begin
  1024. needrtti:=false;
  1025. if is_interfacecom(t) then
  1026. decrfunc:='FPC_INTF_DECR_REF'
  1027. else if is_ansistring(t) then
  1028. decrfunc:='FPC_ANSISTR_DECR_REF'
  1029. else if is_widestring(t) then
  1030. decrfunc:='FPC_WIDESTR_DECR_REF'
  1031. else if is_dynamic_array(t) then
  1032. begin
  1033. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1034. needrtti:=true;
  1035. end
  1036. else
  1037. decrfunc:='';
  1038. { call the special decr function or the generic decref }
  1039. if decrfunc<>'' then
  1040. begin
  1041. if needrtti then
  1042. begin
  1043. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1044. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1045. end;
  1046. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1047. a_call_name(list,decrfunc);
  1048. end
  1049. else
  1050. begin
  1051. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1052. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1053. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1054. a_call_name(list,'FPC_DECREF');
  1055. end;
  1056. end;
  1057. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1058. var
  1059. href : treference;
  1060. begin
  1061. if is_ansistring(t) or
  1062. is_widestring(t) or
  1063. is_interfacecom(t) then
  1064. a_load_const_ref(list,OS_ADDR,0,ref)
  1065. else
  1066. begin
  1067. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1068. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1069. if loadref then
  1070. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1071. else
  1072. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1073. a_call_name(list,'FPC_INITIALIZE');
  1074. end;
  1075. end;
  1076. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1077. var
  1078. href : treference;
  1079. begin
  1080. if is_ansistring(t) or
  1081. is_widestring(t) or
  1082. is_interfacecom(t) then
  1083. g_decrrefcount(list,t,ref)
  1084. else
  1085. begin
  1086. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1087. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1088. if loadref then
  1089. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  1090. else
  1091. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  1092. a_call_name(list,'FPC_FINALIZE');
  1093. end;
  1094. end;
  1095. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  1096. { generate range checking code for the value at location p. The type }
  1097. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1098. { is the original type used at that location. When both defs are equal }
  1099. { the check is also insert (needed for succ,pref,inc,dec) }
  1100. const
  1101. {$ifdef ver1_0}
  1102. awordsignedmax=high(longint);
  1103. {$else}
  1104. awordsignedmax=high(aword) div 2;
  1105. {$endif}
  1106. var
  1107. neglabel : tasmlabel;
  1108. hreg : tregister;
  1109. fromdef : tdef;
  1110. lto,hto,
  1111. lfrom,hfrom : TConstExprInt;
  1112. from_signed: boolean;
  1113. begin
  1114. { range checking on and range checkable value? }
  1115. if not(cs_check_range in aktlocalswitches) or
  1116. not(todef.deftype in [orddef,enumdef,arraydef]) then
  1117. exit;
  1118. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  1119. begin
  1120. cg64.g_rangecheck64(list,p,todef);
  1121. exit;
  1122. end;
  1123. { only check when assigning to scalar, subranges are different, }
  1124. { when todef=fromdef then the check is always generated }
  1125. fromdef:=p.resulttype.def;
  1126. getrange(p.resulttype.def,lfrom,hfrom);
  1127. getrange(todef,lto,hto);
  1128. { no range check if from and to are equal and are both longint/dword }
  1129. { (if we have a 32bit processor) or int64/qword, since such }
  1130. { operations can at most cause overflows (JM) }
  1131. { Note that these checks are mostly processor independent, they only }
  1132. { have to be changed once we introduce 64bit subrange types }
  1133. if (fromdef = todef) and
  1134. (fromdef.deftype=orddef) and
  1135. (((sizeof(aword) = 4) and
  1136. (((torddef(fromdef).typ = s32bit) and
  1137. (lfrom = low(longint)) and
  1138. (hfrom = high(longint))) or
  1139. ((torddef(fromdef).typ = u32bit) and
  1140. (lfrom = low(cardinal)) and
  1141. (hfrom = high(cardinal)))))) then
  1142. exit;
  1143. if todef<>fromdef then
  1144. begin
  1145. { if the from-range falls completely in the to-range, no check }
  1146. { is necessary }
  1147. if (lto<=lfrom) and (hto>=hfrom) then
  1148. exit;
  1149. end;
  1150. { generate the rangecheck code for the def where we are going to }
  1151. { store the result }
  1152. { use the trick that }
  1153. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1154. { To be able to do that, we have to make sure however that either }
  1155. { fromdef and todef are both signed or unsigned, or that we leave }
  1156. { the parts < 0 and > maxlongint out }
  1157. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1158. from_signed := is_signed(fromdef);
  1159. if from_signed xor is_signed(todef) then
  1160. if from_signed then
  1161. { from is signed, to is unsigned }
  1162. begin
  1163. { if high(from) < 0 -> always range error }
  1164. if (hfrom < 0) or
  1165. { if low(to) > maxlongint also range error }
  1166. (lto > awordsignedmax) then
  1167. begin
  1168. a_call_name(list,'FPC_RANGEERROR');
  1169. exit
  1170. end;
  1171. { from is signed and to is unsigned -> when looking at from }
  1172. { as an unsigned value, it must be < maxlongint (otherwise }
  1173. { it's negative, which is invalid since "to" is unsigned) }
  1174. if hto > awordsignedmax then
  1175. hto := awordsignedmax;
  1176. end
  1177. else
  1178. { from is unsigned, to is signed }
  1179. begin
  1180. if (lfrom > awordsignedmax) or
  1181. (hto < 0) then
  1182. begin
  1183. a_call_name(list,'FPC_RANGEERROR');
  1184. exit
  1185. end;
  1186. { from is unsigned and to is signed -> when looking at to }
  1187. { as an unsigned value, it must be >= 0 (since negative }
  1188. { values are the same as values > maxlongint) }
  1189. if lto < 0 then
  1190. lto := 0;
  1191. end;
  1192. hreg := get_scratch_reg_int(list);
  1193. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1194. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  1195. aword(lto),p.location.register,hreg)
  1196. else
  1197. begin
  1198. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  1199. p.location.reference,hreg);
  1200. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  1201. end;
  1202. objectlibrary.getlabel(neglabel);
  1203. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1204. { !!! should happen right after the compare (JM) }
  1205. free_scratch_reg(list,hreg);
  1206. a_call_name(list,'FPC_RANGEERROR');
  1207. a_label(list,neglabel);
  1208. end;
  1209. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1210. begin
  1211. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  1212. a_call_name(list,'FPC_STACKCHECK');
  1213. end;
  1214. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1215. var
  1216. tmpreg : tregister;
  1217. begin
  1218. tmpreg := get_scratch_reg_int(list);
  1219. g_flags2reg(list,size,f,tmpreg);
  1220. a_load_reg_ref(list,size,tmpreg,ref);
  1221. free_scratch_reg(list,tmpreg);
  1222. end;
  1223. procedure tcg.g_maybe_loadself(list : taasmoutput);
  1224. var
  1225. hp : treference;
  1226. p : tprocinfo;
  1227. i : longint;
  1228. spr : Tregister;
  1229. begin
  1230. spr.enum:=SELF_POINTER_REG;
  1231. if assigned(procinfo._class) then
  1232. begin
  1233. list.concat(tai_regalloc.Alloc(spr));
  1234. if lexlevel>normal_function_level then
  1235. begin
  1236. reference_reset_base(hp,procinfo.framepointer,procinfo.framepointer_offset);
  1237. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1238. p:=procinfo.parent;
  1239. for i:=3 to lexlevel-1 do
  1240. begin
  1241. reference_reset_base(hp,spr,p.framepointer_offset);
  1242. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1243. p:=p.parent;
  1244. end;
  1245. reference_reset_base(hp,spr,p.selfpointer_offset);
  1246. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1247. end
  1248. else
  1249. begin
  1250. reference_reset_base(hp,procinfo.framepointer,procinfo.selfpointer_offset);
  1251. a_load_ref_reg(list,OS_ADDR,hp,spr);
  1252. end;
  1253. end;
  1254. end;
  1255. procedure tcg.g_maybe_testself(list : taasmoutput);
  1256. var
  1257. OKLabel : tasmlabel;
  1258. dummyloc : tparalocation;
  1259. spr : Tregister;
  1260. begin
  1261. if (cs_check_object in aktlocalswitches) or
  1262. (cs_check_range in aktlocalswitches) then
  1263. begin
  1264. spr.enum:=SELF_POINTER_REG;
  1265. objectlibrary.getlabel(oklabel);
  1266. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,spr,oklabel);
  1267. a_param_const(list,OS_INT,210,dummyloc);
  1268. a_call_name(list,'FPC_HANDLEERROR');
  1269. a_label(list,oklabel);
  1270. end;
  1271. end;
  1272. {*****************************************************************************
  1273. Entry/Exit Code Functions
  1274. *****************************************************************************}
  1275. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1276. var
  1277. href : treference;
  1278. hregister : tregister;
  1279. spr,acc : Tregister;
  1280. begin
  1281. acc.enum:=accumulator;
  1282. spr.enum:=SELF_POINTER_REG;
  1283. if is_class(procinfo._class) then
  1284. begin
  1285. if (cs_implicit_exceptions in aktmoduleswitches) then
  1286. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1287. { parameter 2 : self pointer / flag }
  1288. {!! this is a terrible hack, normally the helper should get three params : }
  1289. { one with self register, one with flag and one with VMT pointer }
  1290. {reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);}
  1291. a_param_reg(list, OS_ADDR, spr, paramanager.getintparaloc(2));
  1292. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1293. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1294. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1295. a_call_name(list,'FPC_NEW_CLASS');
  1296. a_load_reg_reg(list,OS_ADDR,OS_ADDR,acc,spr);
  1297. { save the self pointer result }
  1298. a_load_reg_ref(list,OS_ADDR,spr,href);
  1299. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1300. end
  1301. else if is_object(procinfo._class) then
  1302. begin
  1303. { parameter 3 :vmt_offset }
  1304. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1305. { parameter 2 : address of pointer to vmt }
  1306. { this is the first(?) parameter which was pushed to the constructor }
  1307. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1308. hregister:=get_scratch_reg_address(list);
  1309. a_loadaddr_ref_reg(list, href, hregister);
  1310. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1311. free_scratch_reg(list, hregister);
  1312. { parameter 1 : address of self pointer }
  1313. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1314. hregister:=get_scratch_reg_address(list);
  1315. a_loadaddr_ref_reg(list, href, hregister);
  1316. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1317. free_scratch_reg(list, hregister);
  1318. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1319. a_load_reg_reg(list,OS_ADDR,OS_ADDR,acc,spr);
  1320. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,acc,faillabel);
  1321. end
  1322. else
  1323. internalerror(200006161);
  1324. end;
  1325. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1326. var
  1327. nofinal : tasmlabel;
  1328. href : treference;
  1329. hregister : tregister;
  1330. spr : Tregister;
  1331. begin
  1332. spr.enum:=SELF_POINTER_REG;
  1333. if is_class(procinfo._class) then
  1334. begin
  1335. { 2nd parameter : flag }
  1336. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset+POINTER_SIZE);
  1337. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1338. { 1st parameter to destructor : self }
  1339. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1340. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1341. a_call_name(list,'FPC_DISPOSE_CLASS')
  1342. end
  1343. else if is_object(procinfo._class) then
  1344. begin
  1345. { must the object be finalized ? }
  1346. if procinfo._class.needs_inittable then
  1347. begin
  1348. objectlibrary.getlabel(nofinal);
  1349. reference_reset_base(href,procinfo.framepointer,target_info.first_parm_offset);
  1350. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1351. reference_reset_base(href,spr,0);
  1352. g_finalize(list,procinfo._class,href,false);
  1353. a_label(list,nofinal);
  1354. end;
  1355. { actually call destructor }
  1356. { parameter 3 :vmt_offset }
  1357. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1358. { parameter 2 : pointer to vmt }
  1359. { this is the first parameter which was pushed to the destructor }
  1360. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1361. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1362. { parameter 1 : address of self pointer }
  1363. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1364. hregister:=get_scratch_reg_address(list);
  1365. a_loadaddr_ref_reg(list, href, hregister);
  1366. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1367. free_scratch_reg(list, hregister);
  1368. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1369. end
  1370. else
  1371. internalerror(200006162);
  1372. end;
  1373. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1374. var
  1375. href : treference;
  1376. hregister : tregister;
  1377. spr : Tregister;
  1378. begin
  1379. spr.enum:=SELF_POINTER_REG;
  1380. if is_class(procinfo._class) then
  1381. begin
  1382. {
  1383. Dispose of the class then set self_pointer to nil
  1384. both in stack and in self register.
  1385. }
  1386. { 2nd parameter : flag }
  1387. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1388. { 1st parameter to destructor : self }
  1389. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1390. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1391. a_call_name(list,'FPC_DISPOSE_CLASS');
  1392. { SET SELF TO NIL }
  1393. a_load_const_reg(list,OS_ADDR,0,spr);
  1394. { set the self pointer in the stack to nil }
  1395. a_load_reg_ref(list,OS_ADDR,spr,href);
  1396. end
  1397. else if is_object(procinfo._class) then
  1398. begin
  1399. { parameter 3 :vmt_offset }
  1400. a_param_const(list, OS_32, procinfo._class.vmt_offset, paramanager.getintparaloc(3));
  1401. { parameter 2 : address of pointer to vmt }
  1402. { this is the first(?) parameter which was pushed to the constructor }
  1403. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset-POINTER_SIZE);
  1404. hregister:=get_scratch_reg_address(list);
  1405. a_loadaddr_ref_reg(list, href, hregister);
  1406. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1407. free_scratch_reg(list, hregister);
  1408. { parameter 1 : address of self pointer }
  1409. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1410. hregister:=get_scratch_reg_address(list);
  1411. a_loadaddr_ref_reg(list, href, hregister);
  1412. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1413. free_scratch_reg(list, hregister);
  1414. a_call_name(list,'FPC_HELP_FAIL');
  1415. { SET SELF TO NIL }
  1416. a_load_const_reg(list,OS_ADDR,0,spr);
  1417. end
  1418. else
  1419. internalerror(200006163);
  1420. end;
  1421. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1422. begin
  1423. end;
  1424. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1425. begin
  1426. end;
  1427. procedure tcg.g_profilecode(list : taasmoutput);
  1428. begin
  1429. end;
  1430. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1431. var r:Tregister;
  1432. begin
  1433. r.enum:=accumulator;
  1434. a_load_reg_ref(list, OS_S32, r, href);
  1435. end;
  1436. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1437. begin
  1438. a_load_const_ref(list, OS_S32, a, href);
  1439. end;
  1440. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1441. var r:Tregister;
  1442. begin
  1443. r.enum:=accumulator;
  1444. a_load_ref_reg(list, OS_S32, href, r);
  1445. end;
  1446. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;
  1447. regsrc,regdst : tregister64);
  1448. begin
  1449. a_load64_reg_reg(list,regsrc,regdst);
  1450. a_op64_const_reg(list,op,value,regdst);
  1451. end;
  1452. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1453. begin
  1454. a_load64_reg_reg(list,regsrc2,regdst);
  1455. a_op64_reg_reg(list,op,regsrc1,regdst);
  1456. end;
  1457. initialization
  1458. ;
  1459. finalization
  1460. cg.free;
  1461. cg64.free;
  1462. end.
  1463. {
  1464. $Log$
  1465. Revision 1.74 2003-01-17 12:45:40 daniel
  1466. * Fixed internalerror 200301081 problem
  1467. Revision 1.73 2003/01/13 14:54:34 daniel
  1468. * Further work to convert codegenerator register convention;
  1469. internalerror bug fixed.
  1470. Revision 1.72 2003/01/09 22:00:53 florian
  1471. * fixed some PowerPC issues
  1472. Revision 1.71 2003/01/09 20:41:10 florian
  1473. * fixed broken PowerPC compiler
  1474. Revision 1.70 2003/01/08 18:43:56 daniel
  1475. * Tregister changed into a record
  1476. Revision 1.69 2002/12/24 15:56:50 peter
  1477. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1478. this for the pageprotection
  1479. Revision 1.68 2002/12/20 18:14:04 peter
  1480. * removed some runerror and writeln
  1481. Revision 1.67 2002/12/14 15:02:03 carl
  1482. * maxoperands -> max_operands (for portability in rautils.pas)
  1483. * fix some range-check errors with loadconst
  1484. + add ncgadd unit to m68k
  1485. * some bugfix of a_param_reg with LOC_CREFERENCE
  1486. Revision 1.66 2002/11/25 17:43:16 peter
  1487. * splitted defbase in defutil,symutil,defcmp
  1488. * merged isconvertable and is_equal into compare_defs(_ext)
  1489. * made operator search faster by walking the list only once
  1490. Revision 1.65 2002/11/17 16:27:31 carl
  1491. * document flags2reg
  1492. Revision 1.64 2002/11/16 17:06:28 peter
  1493. * return error 210 for failed self test
  1494. Revision 1.63 2002/11/15 01:58:46 peter
  1495. * merged changes from 1.0.7 up to 04-11
  1496. - -V option for generating bug report tracing
  1497. - more tracing for option parsing
  1498. - errors for cdecl and high()
  1499. - win32 import stabs
  1500. - win32 records<=8 are returned in eax:edx (turned off by default)
  1501. - heaptrc update
  1502. - more info for temp management in .s file with EXTDEBUG
  1503. Revision 1.62 2002/10/16 19:01:43 peter
  1504. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1505. implicit exception frames for procedures with initialized variables
  1506. and for constructors. The default is on for compatibility
  1507. Revision 1.61 2002/10/05 12:43:23 carl
  1508. * fixes for Delphi 6 compilation
  1509. (warning : Some features do not work under Delphi)
  1510. Revision 1.60 2002/10/02 18:20:52 peter
  1511. * Copy() is now internal syssym that calls compilerprocs
  1512. Revision 1.59 2002/09/17 18:54:02 jonas
  1513. * a_load_reg_reg() now has two size parameters: source and dest. This
  1514. allows some optimizations on architectures that don't encode the
  1515. register size in the register name.
  1516. Revision 1.58 2002/09/09 19:29:29 peter
  1517. * fixed dynarr_decr_ref call
  1518. Revision 1.57 2002/09/07 15:25:01 peter
  1519. * old logs removed and tabs fixed
  1520. Revision 1.56 2002/09/01 21:04:47 florian
  1521. * several powerpc related stuff fixed
  1522. Revision 1.55 2002/09/01 17:05:43 florian
  1523. + added abstract tcg.g_removevaluepara_openarray
  1524. Revision 1.54 2002/09/01 12:09:27 peter
  1525. + a_call_reg, a_call_loc added
  1526. * removed exprasmlist references
  1527. Revision 1.53 2002/08/19 18:17:48 carl
  1528. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  1529. * more fixes to m68k for 64-bit operations
  1530. Revision 1.52 2002/08/17 22:09:43 florian
  1531. * result type handling in tcgcal.pass_2 overhauled
  1532. * better tnode.dowrite
  1533. * some ppc stuff fixed
  1534. Revision 1.51 2002/08/17 09:23:33 florian
  1535. * first part of procinfo rewrite
  1536. Revision 1.50 2002/08/16 14:24:57 carl
  1537. * issameref() to test if two references are the same (then emit no opcodes)
  1538. + ret_in_reg to replace ret_in_acc
  1539. (fix some register allocation bugs at the same time)
  1540. + save_std_register now has an extra parameter which is the
  1541. usedinproc registers
  1542. Revision 1.49 2002/08/15 08:13:54 carl
  1543. - a_load_sym_ofs_reg removed
  1544. * loadvmt now calls loadaddr_ref_reg instead
  1545. Revision 1.48 2002/08/14 19:26:02 carl
  1546. + routine to optimize opcodes with constants
  1547. Revision 1.47 2002/08/11 14:32:26 peter
  1548. * renamed current_library to objectlibrary
  1549. Revision 1.46 2002/08/11 13:24:11 peter
  1550. * saving of asmsymbols in ppu supported
  1551. * asmsymbollist global is removed and moved into a new class
  1552. tasmlibrarydata that will hold the info of a .a file which
  1553. corresponds with a single module. Added librarydata to tmodule
  1554. to keep the library info stored for the module. In the future the
  1555. objectfiles will also be stored to the tasmlibrarydata class
  1556. * all getlabel/newasmsymbol and friends are moved to the new class
  1557. Revision 1.45 2002/08/10 17:15:20 jonas
  1558. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  1559. Revision 1.44 2002/08/09 19:10:05 carl
  1560. - moved new_exception and free_exception to ncgutils
  1561. Revision 1.43 2002/08/05 18:27:48 carl
  1562. + more more more documentation
  1563. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1564. Revision 1.42 2002/08/04 19:08:21 carl
  1565. + added generic exception support (still does not work!)
  1566. + more documentation
  1567. Revision 1.41 2002/07/30 20:50:43 florian
  1568. * the code generator knows now if parameters are in registers
  1569. Revision 1.40 2002/07/29 21:16:02 florian
  1570. * some more ppc fixes
  1571. Revision 1.39 2002/07/28 15:56:00 jonas
  1572. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1573. generic implementation
  1574. Revision 1.38 2002/07/27 19:53:51 jonas
  1575. + generic implementation of tcg.g_flags2ref()
  1576. * tcg.flags2xxx() now also needs a size parameter
  1577. Revision 1.37 2002/07/20 11:57:53 florian
  1578. * types.pas renamed to defbase.pas because D6 contains a types
  1579. unit so this would conflicts if D6 programms are compiled
  1580. + Willamette/SSE2 instructions to assembler added
  1581. }