cgobj.pas 75 KB

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