cgobj.pas 77 KB

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