cgobj.pas 72 KB

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