cgobj.pas 73 KB

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