cgobj.pas 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. {$ifdef delphi}
  32. dmisc,
  33. {$endif}
  34. cclasses,
  35. cpubase,cpuinfo,cgbase,
  36. aasmbase,aasmtai,aasmcpu,
  37. symconst,symbase,symtype,symdef,symtable
  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. alignment : talignment;
  50. t_times:cardinal;
  51. {************************************************}
  52. { basic routines }
  53. constructor create;
  54. {# Initialize the register allocators needed for the codegenerator.}
  55. procedure init_register_allocators;virtual;abstract;
  56. {# Clean up the register allocators needed for the codegenerator.}
  57. procedure done_register_allocators;virtual;abstract;
  58. {# Gets a register suitable to do integer operations on.}
  59. function getintregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;abstract;
  60. {# Gets a register suitable to do integer operations on.}
  61. function getaddressregister(list:Taasmoutput):Tregister;virtual;abstract;
  62. function getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;abstract;
  63. function getabtregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;abstract;
  64. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  65. the cpu specific child cg object have such a method?}
  66. procedure ungetregister(list:Taasmoutput;r:Tregister);virtual;abstract;
  67. procedure ungetreference(list:Taasmoutput;const r:Treference);virtual;abstract;
  68. procedure add_move_instruction(instr:Taicpu);virtual;abstract;
  69. {# Get a specific register.}
  70. procedure getexplicitregister(list:Taasmoutput;r:Tregister);virtual;abstract;
  71. {# Get multiple registers specified.}
  72. procedure allocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tsuperregisterset);virtual;abstract;
  73. {# Free multiple registers specified.}
  74. procedure deallocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tsuperregisterset);virtual;abstract;
  75. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;abstract;
  76. function makeregsize(reg:Tregister;size:Tcgsize):Tregister;
  77. {# Returns the tcgsize corresponding with the size of reg.}
  78. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  79. {# Emit a label to the instruction stream. }
  80. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  81. {# Allocates register r by inserting a pai_realloc record }
  82. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  83. {# Deallocates register r by inserting a pa_regdealloc record}
  84. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  85. {# Pass a parameter, which is located in a register, to a routine.
  86. This routine should push/send the parameter to the routine, as
  87. required by the specific processor ABI and routine modifiers.
  88. This must be overriden for each CPU target.
  89. @param(size size of the operand in the register)
  90. @param(r register source of the operand)
  91. @param(locpara where the parameter will be stored)
  92. }
  93. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);virtual;
  94. {# Pass a parameter, which is a constant, to a routine.
  95. A generic version is provided. This routine should
  96. be overriden for optimization purposes if the cpu
  97. permits directly sending this type of parameter.
  98. @param(size size of the operand in constant)
  99. @param(a value of constant to send)
  100. @param(locpara where the parameter will be stored)
  101. }
  102. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);virtual;
  103. {# Pass the value of a parameter, which is located in memory, to a routine.
  104. A generic version is provided. This routine should
  105. be overriden for optimization purposes if the cpu
  106. permits directly sending this type of parameter.
  107. @param(size size of the operand in constant)
  108. @param(r Memory reference of value to send)
  109. @param(locpara where the parameter will be stored)
  110. }
  111. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);virtual;
  112. {# Pass the value of a parameter, which can be located either in a register or memory location,
  113. to a routine.
  114. A generic version is provided.
  115. @param(l location of the operand to send)
  116. @param(nr parameter number (starting from one) of routine (from left to right))
  117. @param(locpara where the parameter will be stored)
  118. }
  119. procedure a_param_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);
  120. {# Pass the address of a reference to a routine. This routine
  121. will calculate the address of the reference, and pass this
  122. calculated address as a parameter.
  123. A generic version is provided. This routine should
  124. be overriden for optimization purposes if the cpu
  125. permits directly sending this type of parameter.
  126. @param(r reference to get address from)
  127. @param(nr parameter number (starting from one) of routine (from left to right))
  128. }
  129. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);virtual;
  130. { Copies a whole memory block to the stack, the locpara must be a memory location }
  131. procedure a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  132. { Remarks:
  133. * If a method specifies a size you have only to take care
  134. of that number of bits, i.e. load_const_reg with OP_8 must
  135. only load the lower 8 bit of the specified register
  136. the rest of the register can be undefined
  137. if necessary the compiler will call a method
  138. to zero or sign extend the register
  139. * The a_load_XX_XX with OP_64 needn't to be
  140. implemented for 32 bit
  141. processors, the code generator takes care of that
  142. * the addr size is for work with the natural pointer
  143. size
  144. * the procedures without fpu/mm are only for integer usage
  145. * normally the first location is the source and the
  146. second the destination
  147. }
  148. { Copy a parameter to a (temporary) reference }
  149. procedure a_load_param_ref(list : taasmoutput;const locpara : tparalocation;const ref:treference);virtual;
  150. { Copy a parameter to a register }
  151. procedure a_load_param_reg(list : taasmoutput;const locpara : tparalocation;const reg:tregister);virtual;
  152. {# Emits instruction to call the method specified by symbol name.
  153. This routine must be overriden for each new target cpu.
  154. There is no a_call_ref because loading the reference will use
  155. a temp register on most cpu's resulting in conflicts with the
  156. registers used for the parameters (PFV)
  157. }
  158. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  159. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual;abstract;
  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;fromsize,tosize : 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;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  167. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  168. procedure a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  169. procedure a_load_loc_reg(list : taasmoutput;tosize: tcgsize; const loc: tlocation; reg : tregister);
  170. procedure a_load_loc_ref(list : taasmoutput;tosize: tcgsize; 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; size:tcgsize; 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; size: TCGSize; 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 FUNCTION_RETURN_REG. 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 FUNCTION_RETURN_REG. 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 l:tlocation; fromdef,todef: tdef); virtual;
  304. {# Generates overflow checking code for a node }
  305. procedure g_overflowcheck(list: taasmoutput; const l:tlocation; def:tdef); virtual; abstract;
  306. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);virtual;abstract;
  307. {# Emits instructions which should be emitted when entering
  308. a routine declared as @var(interrupt). The default
  309. behavior does nothing, should be overriden as required.
  310. }
  311. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  312. {# Emits instructions which should be emitted when exiting
  313. a routine declared as @var(interrupt). The default
  314. behavior does nothing, should be overriden as required.
  315. }
  316. procedure g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);virtual;
  317. {# Emits instructions when compilation is done in profile
  318. mode (this is set as a command line option). The default
  319. behavior does nothing, should be overriden as required.
  320. }
  321. procedure g_profilecode(list : taasmoutput);virtual;
  322. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  323. @param(size Number of bytes to allocate)
  324. }
  325. procedure g_stackpointer_alloc(list : taasmoutput;size : longint);virtual; abstract;
  326. {# Emits instruction for allocating the locals in entry
  327. code of a routine. This is one of the first
  328. routine called in @var(genentrycode).
  329. @param(localsize Number of bytes to allocate as locals)
  330. }
  331. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  332. {# Emits instructiona for restoring the frame pointer
  333. at routine exit. For some processors, this routine
  334. may do nothing at all.
  335. }
  336. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  337. {# Emits instructions for returning from a subroutine.
  338. Should also restore the stack.
  339. @param(parasize Number of bytes of parameters to deallocate from stack)
  340. }
  341. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  342. {# This routine is called when generating the code for the entry point
  343. of a routine. It should save all registers which are not used in this
  344. routine, and which should be declared as saved in the std_saved_registers
  345. set.
  346. This routine is mainly used when linking to code which is generated
  347. by ABI-compliant compilers (like GCC), to make sure that the reserved
  348. registers of that ABI are not clobbered.
  349. @param(usedinproc Registers which are used in the code of this routine)
  350. }
  351. procedure g_save_standard_registers(list:Taasmoutput);virtual;abstract;
  352. {# This routine is called when generating the code for the exit point
  353. of a routine. It should restore all registers which were previously
  354. saved in @var(g_save_standard_registers).
  355. @param(usedinproc Registers which are used in the code of this routine)
  356. }
  357. procedure g_restore_standard_registers(list:Taasmoutput);virtual;abstract;
  358. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  359. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);virtual;abstract;
  360. end;
  361. {# @abstract(Abstract code generator for 64 Bit operations)
  362. This class implements an abstract code generator class
  363. for 64 Bit operations.
  364. }
  365. tcg64 = class
  366. { Allocates 64 Bit register r by inserting a pai_realloc record }
  367. procedure a_reg_alloc(list : taasmoutput;r : tregister64);virtual;abstract;
  368. { Deallocates 64 Bit register r by inserting a pa_regdealloc record}
  369. procedure a_reg_dealloc(list : taasmoutput;r : tregister64);virtual;abstract;
  370. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);virtual;abstract;
  371. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  372. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64;delete : boolean);virtual;abstract;
  373. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64;delete : boolean);virtual;abstract;
  374. procedure a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);virtual;abstract;
  375. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64;delete : boolean);virtual;abstract;
  376. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  377. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);virtual;abstract;
  378. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  379. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  380. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  381. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  382. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  383. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  384. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  385. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  386. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  387. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  388. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;regdst : tregister64);virtual;abstract;
  389. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);virtual;abstract;
  390. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);virtual;abstract;
  391. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  392. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  393. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);virtual;
  394. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  395. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  396. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  397. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  398. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  399. {
  400. This routine tries to optimize the const_reg opcode, and should be
  401. called at the start of a_op64_const_reg. It returns the actual opcode
  402. to emit, and the constant value to emit. If this routine returns
  403. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  404. @param(op The opcode to emit, returns the opcode which must be emitted)
  405. @param(a The constant which should be emitted, returns the constant which must
  406. be emitted)
  407. @param(reg The register to emit the opcode with, returns the register with
  408. which the opcode will be emitted)
  409. }
  410. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;virtual;abstract;
  411. { override to catch 64bit rangechecks }
  412. procedure g_rangecheck64(list: taasmoutput; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  413. end;
  414. var
  415. {# Main code generator class }
  416. cg : tcg;
  417. {# Code generator class for all operations working with 64-Bit operands }
  418. cg64 : tcg64;
  419. implementation
  420. uses
  421. globals,globtype,options,systems,
  422. verbose,defutil,paramgr,symsym,
  423. rgobj,cutils;
  424. const
  425. { Please leave this here, this module should NOT use
  426. exprasmlist, the lists are always passed as arguments.
  427. Declaring it as string here results in an error when compiling (PFV) }
  428. exprasmlist = 'error';
  429. {*****************************************************************************
  430. basic functionallity
  431. ******************************************************************************}
  432. constructor tcg.create;
  433. begin
  434. end;
  435. function Tcg.makeregsize(reg:Tregister;size:Tcgsize):Tregister;
  436. var subreg:Tsubregister;
  437. begin
  438. subreg:=cgsize2subreg(size);
  439. result:=reg;
  440. setsubreg(result,subreg);
  441. end;
  442. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  443. begin
  444. list.concat(tai_regalloc.alloc(r));
  445. end;
  446. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  447. begin
  448. list.concat(tai_regalloc.dealloc(r));
  449. end;
  450. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  451. begin
  452. list.concat(tai_label.create(l));
  453. end;
  454. {*****************************************************************************
  455. for better code generation these methods should be overridden
  456. ******************************************************************************}
  457. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  458. var
  459. ref : treference;
  460. begin
  461. case locpara.loc of
  462. LOC_REGISTER,LOC_CREGISTER:
  463. a_load_reg_reg(list,size,locpara.size,r,locpara.register);
  464. LOC_REFERENCE,LOC_CREFERENCE:
  465. begin
  466. reference_reset(ref);
  467. ref.base:=locpara.reference.index;
  468. ref.offset:=locpara.reference.offset;
  469. a_load_reg_ref(list,size,size,r,ref);
  470. end
  471. else
  472. internalerror(2002071004);
  473. end;
  474. end;
  475. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  476. var
  477. hr : tregister;
  478. begin
  479. hr:=getintregister(list,size);
  480. a_load_const_reg(list,size,a,hr);
  481. a_param_reg(list,size,hr,locpara);
  482. ungetregister(list,hr);
  483. end;
  484. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  485. var
  486. hr : tregister;
  487. begin
  488. hr:=getintregister(list,size);
  489. a_load_ref_reg(list,size,size,r,hr);
  490. a_param_reg(list,size,hr,locpara);
  491. ungetregister(list,hr);
  492. end;
  493. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  494. begin
  495. case l.loc of
  496. LOC_REGISTER,
  497. LOC_CREGISTER :
  498. a_param_reg(list,l.size,l.register,locpara);
  499. LOC_CONSTANT :
  500. a_param_const(list,l.size,l.value,locpara);
  501. LOC_CREFERENCE,
  502. LOC_REFERENCE :
  503. a_param_ref(list,l.size,l.reference,locpara);
  504. else
  505. internalerror(2002032211);
  506. end;
  507. end;
  508. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  509. var
  510. hr : tregister;
  511. begin
  512. hr:=getaddressregister(list);
  513. a_loadaddr_ref_reg(list,r,hr);
  514. a_param_reg(list,OS_ADDR,hr,locpara);
  515. ungetregister(list,hr);
  516. end;
  517. procedure tcg.a_param_copy_ref(list : taasmoutput;size : qword;const r : treference;const locpara : tparalocation);
  518. var
  519. ref : treference;
  520. begin
  521. if not(locpara.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  522. internalerror(2003010901);
  523. reference_reset_base(ref,locpara.reference.index,locpara.reference.offset);
  524. g_concatcopy(list,r,ref,size,false,false);
  525. end;
  526. procedure tcg.a_load_param_ref(list : taasmoutput;const locpara : tparalocation;const ref:treference);
  527. begin
  528. case locpara.loc of
  529. LOC_CREGISTER,
  530. LOC_REGISTER:
  531. begin
  532. if (locpara.size in [OS_S64,OS_64]) then
  533. begin
  534. {$ifdef cpu64bit}
  535. ungetregisterint(list,locpara.register64);
  536. {$else cpu64bit}
  537. getexplicitregister(list,locpara.registerlow);
  538. getexplicitregister(list,locpara.registerhigh);
  539. ungetregister(list,locpara.registerlow);
  540. ungetregister(list,locpara.registerhigh);
  541. {$endif cpu64bit}
  542. cg64.a_load64_reg_ref(list,locpara.register64,ref)
  543. end
  544. else
  545. begin
  546. getexplicitregister(list,locpara.register);
  547. ungetregister(list,locpara.register);
  548. a_load_reg_ref(list,locpara.size,locpara.size,locpara.register,ref);
  549. end;
  550. end;
  551. LOC_FPUREGISTER,
  552. LOC_CFPUREGISTER:
  553. a_loadfpu_reg_ref(list,locpara.size,locpara.register,ref);
  554. else
  555. internalerror(2002081302);
  556. end;
  557. end;
  558. procedure tcg.a_load_param_reg(list : taasmoutput;const locpara : tparalocation;const reg:tregister);
  559. var
  560. href : treference;
  561. begin
  562. case locpara.loc of
  563. LOC_CREGISTER,
  564. LOC_REGISTER:
  565. begin
  566. if not(locpara.size in [OS_S64,OS_64]) then
  567. begin
  568. getexplicitregister(list,locpara.register);
  569. ungetregister(list,locpara.register);
  570. getexplicitregister(list,reg);
  571. a_load_reg_reg(list,locpara.size,locpara.size,locpara.register,reg)
  572. end
  573. else
  574. internalerror(2003053011);
  575. end;
  576. LOC_CFPUREGISTER,
  577. LOC_FPUREGISTER:
  578. a_loadfpu_reg_reg(list,locpara.size,locpara.register,reg);
  579. LOC_REFERENCE,
  580. LOC_CREFERENCE:
  581. begin
  582. reference_reset_base(href,locpara.reference.index,locpara.reference.offset);
  583. getexplicitregister(list,reg);
  584. a_load_ref_reg(list,locpara.size,locpara.size,href,reg);
  585. end;
  586. else
  587. internalerror(2003053010);
  588. end
  589. end;
  590. {****************************************************************************
  591. some generic implementations
  592. ****************************************************************************}
  593. procedure tcg.a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  594. var
  595. tmpreg: tregister;
  596. begin
  597. { verify if we have the same reference }
  598. if references_equal(sref,dref) then
  599. exit;
  600. tmpreg:=getintregister(list,tosize);
  601. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  602. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  603. ungetregister(list,tmpreg);
  604. end;
  605. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  606. var
  607. tmpreg: tregister;
  608. begin
  609. tmpreg:=getintregister(list,size);
  610. a_load_const_reg(list,size,a,tmpreg);
  611. a_load_reg_ref(list,size,size,tmpreg,ref);
  612. ungetregister(list,tmpreg);
  613. end;
  614. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  615. begin
  616. case loc.loc of
  617. LOC_REFERENCE,LOC_CREFERENCE:
  618. a_load_const_ref(list,loc.size,a,loc.reference);
  619. LOC_REGISTER,LOC_CREGISTER:
  620. a_load_const_reg(list,loc.size,a,loc.register);
  621. else
  622. internalerror(200203272);
  623. end;
  624. end;
  625. procedure tcg.a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  626. begin
  627. case loc.loc of
  628. LOC_REFERENCE,LOC_CREFERENCE:
  629. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  630. LOC_REGISTER,LOC_CREGISTER:
  631. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  632. else
  633. internalerror(200203271);
  634. end;
  635. end;
  636. procedure tcg.a_load_loc_reg(list : taasmoutput; tosize: tcgsize; const loc: tlocation; reg : tregister);
  637. begin
  638. case loc.loc of
  639. LOC_REFERENCE,LOC_CREFERENCE:
  640. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  641. LOC_REGISTER,LOC_CREGISTER:
  642. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  643. LOC_CONSTANT:
  644. a_load_const_reg(list,tosize,loc.value,reg);
  645. else
  646. internalerror(200109092);
  647. end;
  648. end;
  649. procedure tcg.a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  650. begin
  651. case loc.loc of
  652. LOC_REFERENCE,LOC_CREFERENCE:
  653. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  654. LOC_REGISTER,LOC_CREGISTER:
  655. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  656. LOC_CONSTANT:
  657. a_load_const_ref(list,tosize,loc.value,ref);
  658. else
  659. internalerror(200109302);
  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.size,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,size,reg,loc.register);
  732. else
  733. internalerror(48991);
  734. end;
  735. end;
  736. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const locpara : tparalocation);
  737. var
  738. ref : treference;
  739. begin
  740. case locpara.loc of
  741. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  742. a_loadfpu_reg_reg(list,size,r,locpara.register);
  743. LOC_REFERENCE,LOC_CREFERENCE:
  744. begin
  745. reference_reset(ref);
  746. ref.base:=locpara.reference.index;
  747. ref.offset:=locpara.reference.offset;
  748. a_loadfpu_reg_ref(list,size,r,ref);
  749. end
  750. else
  751. internalerror(2002071004);
  752. end;
  753. end;
  754. procedure tcg.a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const locpara : tparalocation);
  755. var
  756. hr : tregister;
  757. begin
  758. hr:=getfpuregister(list,size);
  759. a_loadfpu_ref_reg(list,size,ref,hr);
  760. a_paramfpu_reg(list,size,hr,locpara);
  761. ungetregister(list,hr);
  762. end;
  763. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  764. var
  765. tmpreg: tregister;
  766. begin
  767. tmpreg:=getintregister(list,size);
  768. a_load_ref_reg(list,size,size,ref,tmpreg);
  769. a_op_const_reg(list,op,size,a,tmpreg);
  770. a_load_reg_ref(list,size,size,tmpreg,ref);
  771. ungetregister(list,tmpreg);
  772. end;
  773. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  774. begin
  775. case loc.loc of
  776. LOC_REGISTER, LOC_CREGISTER:
  777. a_op_const_reg(list,op,loc.size,a,loc.register);
  778. LOC_REFERENCE, LOC_CREFERENCE:
  779. a_op_const_ref(list,op,loc.size,a,loc.reference);
  780. else
  781. internalerror(200109061);
  782. end;
  783. end;
  784. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  785. var
  786. tmpreg: tregister;
  787. begin
  788. tmpreg:=getintregister(list,size);
  789. a_load_ref_reg(list,size,size,ref,tmpreg);
  790. a_op_reg_reg(list,op,size,reg,tmpreg);
  791. a_load_reg_ref(list,size,size,tmpreg,ref);
  792. ungetregister(list,tmpreg);
  793. end;
  794. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  795. var
  796. tmpreg: tregister;
  797. begin
  798. case op of
  799. OP_NOT,OP_NEG:
  800. { handle it as "load ref,reg; op reg" }
  801. begin
  802. a_load_ref_reg(list,size,size,ref,reg);
  803. a_op_reg_reg(list,op,size,reg,reg);
  804. end;
  805. else
  806. begin
  807. tmpreg:=getintregister(list,size);
  808. a_load_ref_reg(list,size,size,ref,tmpreg);
  809. a_op_reg_reg(list,op,size,tmpreg,reg);
  810. ungetregister(list,tmpreg);
  811. end;
  812. end;
  813. end;
  814. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  815. begin
  816. case loc.loc of
  817. LOC_REGISTER, LOC_CREGISTER:
  818. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  819. LOC_REFERENCE, LOC_CREFERENCE:
  820. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  821. else
  822. internalerror(200109061);
  823. end;
  824. end;
  825. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  826. var
  827. tmpreg: tregister;
  828. begin
  829. case loc.loc of
  830. LOC_REGISTER,LOC_CREGISTER:
  831. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  832. LOC_REFERENCE,LOC_CREFERENCE:
  833. begin
  834. tmpreg:=getintregister(list,loc.size);
  835. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  836. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  837. ungetregister(list,tmpreg);
  838. end;
  839. else
  840. internalerror(200109061);
  841. end;
  842. end;
  843. procedure Tcg.a_op_const_reg_reg(list:Taasmoutput;op:Topcg;size:Tcgsize;
  844. a:aword;src,dst:Tregister);
  845. begin
  846. a_load_reg_reg(list,size,size,src,dst);
  847. a_op_const_reg(list,op,size,a,dst);
  848. end;
  849. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  850. size: tcgsize; src1, src2, dst: tregister);
  851. var
  852. tmpreg: tregister;
  853. begin
  854. if (dst<>src1) then
  855. begin
  856. a_load_reg_reg(list,size,size,src2,dst);
  857. a_op_reg_reg(list,op,size,src1,dst);
  858. end
  859. else
  860. begin
  861. tmpreg:=cg.getintregister(list,size);
  862. a_load_reg_reg(list,size,size,src2,tmpreg);
  863. a_op_reg_reg(list,op,size,src1,tmpreg);
  864. a_load_reg_reg(list,size,size,tmpreg,dst);
  865. ungetregister(list,tmpreg);
  866. end;
  867. end;
  868. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  869. l : tasmlabel);
  870. var
  871. tmpreg: tregister;
  872. begin
  873. tmpreg:=getintregister(list,size);
  874. a_load_ref_reg(list,size,size,ref,tmpreg);
  875. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  876. ungetregister(list,tmpreg);
  877. end;
  878. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  879. l : tasmlabel);
  880. begin
  881. case loc.loc of
  882. LOC_REGISTER,LOC_CREGISTER:
  883. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  884. LOC_REFERENCE,LOC_CREFERENCE:
  885. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  886. else
  887. internalerror(200109061);
  888. end;
  889. end;
  890. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  891. var
  892. tmpreg: tregister;
  893. begin
  894. tmpreg:=getintregister(list,size);
  895. a_load_ref_reg(list,size,size,ref,tmpreg);
  896. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  897. ungetregister(list,tmpreg);
  898. end;
  899. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  900. begin
  901. case loc.loc of
  902. LOC_REGISTER,
  903. LOC_CREGISTER:
  904. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  905. LOC_REFERENCE,
  906. LOC_CREFERENCE :
  907. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  908. LOC_CONSTANT:
  909. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  910. else
  911. internalerror(200203231);
  912. end;
  913. end;
  914. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  915. l : tasmlabel);
  916. var
  917. tmpreg: tregister;
  918. begin
  919. case loc.loc of
  920. LOC_REGISTER,LOC_CREGISTER:
  921. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  922. LOC_REFERENCE,LOC_CREFERENCE:
  923. begin
  924. tmpreg:=getintregister(list,size);
  925. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  926. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  927. ungetregister(list,tmpreg);
  928. end
  929. else
  930. internalerror(200109061);
  931. end;
  932. end;
  933. class function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  934. begin
  935. reg_cgsize := OS_INT;
  936. end;
  937. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  938. var
  939. paraloc1,paraloc2,paraloc3 : tparalocation;
  940. begin
  941. {$ifdef FPC}
  942. {$warning FIX ME!}
  943. {$endif}
  944. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  945. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  946. paraloc3:=paramanager.getintparaloc(pocall_default,3);
  947. paramanager.allocparaloc(list,paraloc3);
  948. a_paramaddr_ref(list,dest,paraloc3);
  949. paramanager.allocparaloc(list,paraloc2);
  950. if loadref then
  951. a_param_ref(list,OS_ADDR,source,paraloc2)
  952. else
  953. a_paramaddr_ref(list,source,paraloc2);
  954. if delsource then
  955. reference_release(list,source);
  956. paramanager.allocparaloc(list,paraloc1);
  957. a_param_const(list,OS_INT,len,paraloc1);
  958. paramanager.freeparaloc(list,paraloc3);
  959. paramanager.freeparaloc(list,paraloc2);
  960. paramanager.freeparaloc(list,paraloc1);
  961. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  962. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  963. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  964. end;
  965. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference;loadref : boolean);
  966. var
  967. href : treference;
  968. incrfunc : string;
  969. paraloc1,paraloc2 : tparalocation;
  970. begin
  971. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  972. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  973. { These functions should not change the registers (they use
  974. the saveregister proc directive }
  975. if is_interfacecom(t) then
  976. incrfunc:='FPC_INTF_INCR_REF'
  977. else if is_ansistring(t) then
  978. incrfunc:='FPC_ANSISTR_INCR_REF'
  979. else if is_widestring(t) then
  980. incrfunc:='FPC_WIDESTR_INCR_REF'
  981. else if is_dynamic_array(t) then
  982. incrfunc:='FPC_DYNARRAY_INCR_REF'
  983. else
  984. incrfunc:='';
  985. { call the special incr function or the generic addref }
  986. if incrfunc<>'' then
  987. begin
  988. { these functions get the pointer by value }
  989. paramanager.allocparaloc(list,paraloc1);
  990. a_param_ref(list,OS_ADDR,ref,paraloc1);
  991. paramanager.freeparaloc(list,paraloc1);
  992. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  993. a_call_name(list,incrfunc);
  994. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  995. end
  996. else
  997. begin
  998. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  999. paramanager.allocparaloc(list,paraloc2);
  1000. a_paramaddr_ref(list,href,paraloc2);
  1001. paramanager.allocparaloc(list,paraloc1);
  1002. if loadref then
  1003. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1004. else
  1005. a_paramaddr_ref(list,ref,paraloc1);
  1006. paramanager.freeparaloc(list,paraloc1);
  1007. paramanager.freeparaloc(list,paraloc2);
  1008. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1009. a_call_name(list,'FPC_ADDREF');
  1010. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1011. end;
  1012. end;
  1013. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference; loadref:boolean);
  1014. var
  1015. href : treference;
  1016. decrfunc : string;
  1017. needrtti : boolean;
  1018. paraloc1,paraloc2 : tparalocation;
  1019. begin
  1020. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1021. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1022. needrtti:=false;
  1023. if is_interfacecom(t) then
  1024. decrfunc:='FPC_INTF_DECR_REF'
  1025. else if is_ansistring(t) then
  1026. decrfunc:='FPC_ANSISTR_DECR_REF'
  1027. else if is_widestring(t) then
  1028. decrfunc:='FPC_WIDESTR_DECR_REF'
  1029. else if is_dynamic_array(t) then
  1030. begin
  1031. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1032. needrtti:=true;
  1033. end
  1034. else
  1035. decrfunc:='';
  1036. { call the special decr function or the generic decref }
  1037. if decrfunc<>'' then
  1038. begin
  1039. if needrtti then
  1040. begin
  1041. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1042. paramanager.allocparaloc(list,paraloc2);
  1043. a_paramaddr_ref(list,href,paraloc2);
  1044. end;
  1045. paramanager.allocparaloc(list,paraloc1);
  1046. if loadref then
  1047. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1048. else
  1049. a_paramaddr_ref(list,ref,paraloc1);
  1050. paramanager.freeparaloc(list,paraloc1);
  1051. if needrtti then
  1052. paramanager.freeparaloc(list,paraloc2);
  1053. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1054. a_call_name(list,decrfunc);
  1055. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1056. end
  1057. else
  1058. begin
  1059. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1060. paramanager.allocparaloc(list,paraloc2);
  1061. a_paramaddr_ref(list,href,paraloc2);
  1062. paramanager.allocparaloc(list,paraloc1);
  1063. if loadref then
  1064. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1065. else
  1066. a_paramaddr_ref(list,ref,paraloc1);
  1067. paramanager.freeparaloc(list,paraloc1);
  1068. paramanager.freeparaloc(list,paraloc2);
  1069. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1070. a_call_name(list,'FPC_DECREF');
  1071. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1072. end;
  1073. end;
  1074. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1075. var
  1076. href : treference;
  1077. paraloc1,paraloc2 : tparalocation;
  1078. begin
  1079. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1080. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1081. if is_ansistring(t) or
  1082. is_widestring(t) or
  1083. is_interfacecom(t) then
  1084. a_load_const_ref(list,OS_ADDR,0,ref)
  1085. else
  1086. begin
  1087. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1088. paramanager.allocparaloc(list,paraloc2);
  1089. a_paramaddr_ref(list,href,paraloc2);
  1090. paramanager.allocparaloc(list,paraloc1);
  1091. if loadref then
  1092. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1093. else
  1094. a_paramaddr_ref(list,ref,paraloc1);
  1095. paramanager.freeparaloc(list,paraloc1);
  1096. paramanager.freeparaloc(list,paraloc2);
  1097. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1098. a_call_name(list,'FPC_INITIALIZE');
  1099. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1100. end;
  1101. end;
  1102. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  1103. var
  1104. href : treference;
  1105. paraloc1,paraloc2 : tparalocation;
  1106. begin
  1107. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1108. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1109. if is_ansistring(t) or
  1110. is_widestring(t) or
  1111. is_interfacecom(t) then
  1112. g_decrrefcount(list,t,ref,loadref)
  1113. else
  1114. begin
  1115. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1116. paramanager.allocparaloc(list,paraloc2);
  1117. a_paramaddr_ref(list,href,paraloc2);
  1118. paramanager.allocparaloc(list,paraloc1);
  1119. if loadref then
  1120. a_param_ref(list,OS_ADDR,ref,paraloc1)
  1121. else
  1122. a_paramaddr_ref(list,ref,paraloc1);
  1123. paramanager.freeparaloc(list,paraloc1);
  1124. paramanager.freeparaloc(list,paraloc2);
  1125. allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1126. a_call_name(list,'FPC_FINALIZE');
  1127. deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1128. end;
  1129. end;
  1130. procedure tcg.g_rangecheck(list: taasmoutput; const l:tlocation;fromdef,todef: tdef);
  1131. { generate range checking code for the value at location p. The type }
  1132. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1133. { is the original type used at that location. When both defs are equal }
  1134. { the check is also insert (needed for succ,pref,inc,dec) }
  1135. const
  1136. {$ifdef ver1_0}
  1137. awordsignedmax=high(longint);
  1138. {$else}
  1139. awordsignedmax=high(aword) div 2;
  1140. {$endif}
  1141. var
  1142. neglabel : tasmlabel;
  1143. hreg : tregister;
  1144. lto,hto,
  1145. lfrom,hfrom : TConstExprInt;
  1146. from_signed: boolean;
  1147. begin
  1148. { range checking on and range checkable value? }
  1149. if not(cs_check_range in aktlocalswitches) or
  1150. not(fromdef.deftype in [orddef,enumdef,arraydef]) then
  1151. exit;
  1152. if is_64bit(fromdef) or is_64bit(todef) then
  1153. begin
  1154. cg64.g_rangecheck64(list,l,fromdef,todef);
  1155. exit;
  1156. end;
  1157. { only check when assigning to scalar, subranges are different, }
  1158. { when todef=fromdef then the check is always generated }
  1159. getrange(fromdef,lfrom,hfrom);
  1160. getrange(todef,lto,hto);
  1161. { no range check if from and to are equal and are both longint/dword }
  1162. { (if we have a 32bit processor) or int64/qword, since such }
  1163. { operations can at most cause overflows (JM) }
  1164. { Note that these checks are mostly processor independent, they only }
  1165. { have to be changed once we introduce 64bit subrange types }
  1166. if (fromdef = todef) and
  1167. (fromdef.deftype=orddef) and
  1168. (((sizeof(aword) = 4) and
  1169. (((torddef(fromdef).typ = s32bit) and
  1170. (lfrom = low(longint)) and
  1171. (hfrom = high(longint))) or
  1172. ((torddef(fromdef).typ = u32bit) and
  1173. (lfrom = low(cardinal)) and
  1174. (hfrom = high(cardinal)))))) then
  1175. exit;
  1176. if todef<>fromdef then
  1177. begin
  1178. { if the from-range falls completely in the to-range, no check }
  1179. { is necessary }
  1180. if (lto<=lfrom) and (hto>=hfrom) then
  1181. exit;
  1182. end;
  1183. { generate the rangecheck code for the def where we are going to }
  1184. { store the result }
  1185. { use the trick that }
  1186. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1187. { To be able to do that, we have to make sure however that either }
  1188. { fromdef and todef are both signed or unsigned, or that we leave }
  1189. { the parts < 0 and > maxlongint out }
  1190. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1191. from_signed := is_signed(fromdef);
  1192. if from_signed xor is_signed(todef) then
  1193. if from_signed then
  1194. { from is signed, to is unsigned }
  1195. begin
  1196. { if high(from) < 0 -> always range error }
  1197. if (hfrom < 0) or
  1198. { if low(to) > maxlongint also range error }
  1199. (lto > awordsignedmax) then
  1200. begin
  1201. a_call_name(list,'FPC_RANGEERROR');
  1202. exit
  1203. end;
  1204. { from is signed and to is unsigned -> when looking at from }
  1205. { as an unsigned value, it must be < maxlongint (otherwise }
  1206. { it's negative, which is invalid since "to" is unsigned) }
  1207. if hto > awordsignedmax then
  1208. hto := awordsignedmax;
  1209. end
  1210. else
  1211. { from is unsigned, to is signed }
  1212. begin
  1213. if (lfrom > awordsignedmax) or
  1214. (hto < 0) then
  1215. begin
  1216. a_call_name(list,'FPC_RANGEERROR');
  1217. exit
  1218. end;
  1219. { from is unsigned and to is signed -> when looking at to }
  1220. { as an unsigned value, it must be >= 0 (since negative }
  1221. { values are the same as values > maxlongint) }
  1222. if lto < 0 then
  1223. lto := 0;
  1224. end;
  1225. hreg:=getintregister(list,OS_INT);
  1226. a_load_loc_reg(list,OS_INT,l,hreg);
  1227. a_op_const_reg(list,OP_SUB,OS_INT,aword(lto),hreg);
  1228. objectlibrary.getlabel(neglabel);
  1229. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  1230. { !!! should happen right after the compare (JM) }
  1231. ungetregister(list,hreg);
  1232. a_call_name(list,'FPC_RANGEERROR');
  1233. a_label(list,neglabel);
  1234. end;
  1235. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  1236. var
  1237. paraloc1 : tparalocation;
  1238. begin
  1239. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1240. paramanager.allocparaloc(list,paraloc1);
  1241. a_param_const(list,OS_32,stackframesize,paraloc1);
  1242. paramanager.freeparaloc(list,paraloc1);
  1243. { No register saving needed, saveregisters is used }
  1244. a_call_name(list,'FPC_STACKCHECK');
  1245. end;
  1246. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1247. var
  1248. tmpreg : tregister;
  1249. begin
  1250. tmpreg:=getintregister(list,size);
  1251. g_flags2reg(list,size,f,tmpreg);
  1252. a_load_reg_ref(list,size,size,tmpreg,ref);
  1253. ungetregister(list,tmpreg);
  1254. end;
  1255. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1256. var
  1257. OKLabel : tasmlabel;
  1258. paraloc1 : tparalocation;
  1259. begin
  1260. if (cs_check_object in aktlocalswitches) or
  1261. (cs_check_range in aktlocalswitches) then
  1262. begin
  1263. objectlibrary.getlabel(oklabel);
  1264. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1265. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1266. paramanager.allocparaloc(list,paraloc1);
  1267. a_param_const(list,OS_INT,210,paraloc1);
  1268. paramanager.freeparaloc(list,paraloc1);
  1269. a_call_name(list,'FPC_HANDLEERROR');
  1270. a_label(list,oklabel);
  1271. end;
  1272. end;
  1273. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1274. var
  1275. hrefvmt : treference;
  1276. paraloc1,paraloc2 : tparalocation;
  1277. begin
  1278. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1279. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1280. if (cs_check_object in aktlocalswitches) then
  1281. begin
  1282. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymboldata(objdef.vmt_mangledname),0);
  1283. paramanager.allocparaloc(list,paraloc2);
  1284. a_paramaddr_ref(list,hrefvmt,paraloc2);
  1285. paramanager.allocparaloc(list,paraloc1);
  1286. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1287. paramanager.freeparaloc(list,paraloc1);
  1288. paramanager.freeparaloc(list,paraloc2);
  1289. { No register saving needed, saveregisters is used }
  1290. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1291. end
  1292. else
  1293. if (cs_check_range in aktlocalswitches) then
  1294. begin
  1295. paramanager.allocparaloc(list,paraloc1);
  1296. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1297. paramanager.freeparaloc(list,paraloc1);
  1298. { No register saving needed, saveregisters is used }
  1299. a_call_name(list,'FPC_CHECK_OBJECT');
  1300. end;
  1301. end;
  1302. {*****************************************************************************
  1303. Entry/Exit Code Functions
  1304. *****************************************************************************}
  1305. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1306. begin
  1307. end;
  1308. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);
  1309. begin
  1310. end;
  1311. procedure tcg.g_profilecode(list : taasmoutput);
  1312. begin
  1313. end;
  1314. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1315. begin
  1316. a_load_reg_ref(list, OS_S32, OS_32, NR_FUNCTION_RETURN_REG, href);
  1317. end;
  1318. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);
  1319. begin
  1320. a_load_const_ref(list, OS_S32, a, href);
  1321. end;
  1322. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1323. begin
  1324. a_load_ref_reg(list, OS_S32, OS_S32, href, NR_FUNCTION_RETURN_REG);
  1325. end;
  1326. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword; regsrc,regdst : tregister64);
  1327. begin
  1328. a_load64_reg_reg(list,regsrc,regdst,false);
  1329. a_op64_const_reg(list,op,value,regdst);
  1330. end;
  1331. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1332. begin
  1333. a_load64_reg_reg(list,regsrc2,regdst,false);
  1334. a_op64_reg_reg(list,op,regsrc1,regdst);
  1335. end;
  1336. initialization
  1337. ;
  1338. finalization
  1339. cg.free;
  1340. cg64.free;
  1341. end.
  1342. {
  1343. $Log$
  1344. Revision 1.127 2003-10-09 21:31:37 daniel
  1345. * Register allocator splitted, ans abstract now
  1346. Revision 1.126 2003/10/01 20:34:48 peter
  1347. * procinfo unit contains tprocinfo
  1348. * cginfo renamed to cgbase
  1349. * moved cgmessage to verbose
  1350. * fixed ppc and sparc compiles
  1351. Revision 1.125 2003/09/28 17:55:03 peter
  1352. * parent framepointer changed to hidden parameter
  1353. * tloadparentfpnode added
  1354. Revision 1.124 2003/09/28 13:40:13 peter
  1355. * a_call_ref removed
  1356. Revision 1.123 2003/09/25 21:26:24 peter
  1357. * remove obsolete tparalocation.sp_fixup
  1358. Revision 1.122 2003/09/23 20:37:16 peter
  1359. * fpc_check_object(_ext) has saveregisters and doesn't need
  1360. saving of registers
  1361. Revision 1.121 2003/09/10 08:31:47 marco
  1362. * Patch from Peter for paraloc
  1363. Revision 1.120 2003/09/09 20:59:27 daniel
  1364. * Adding register allocation order
  1365. Revision 1.119 2003/09/07 22:09:34 peter
  1366. * preparations for different default calling conventions
  1367. * various RA fixes
  1368. Revision 1.118 2003/09/03 15:55:00 peter
  1369. * NEWRA branch merged
  1370. Revision 1.117 2003/09/03 11:18:36 florian
  1371. * fixed arm concatcopy
  1372. + arm support in the common compiler sources added
  1373. * moved some generic cg code around
  1374. + tfputype added
  1375. * ...
  1376. Revision 1.116.2.4 2003/08/29 17:28:59 peter
  1377. * next batch of updates
  1378. Revision 1.116.2.3 2003/08/28 18:35:07 peter
  1379. * tregister changed to cardinal
  1380. Revision 1.116.2.2 2003/08/27 20:23:55 peter
  1381. * remove old ra code
  1382. Revision 1.116.2.1 2003/08/27 19:55:54 peter
  1383. * first tregister patch
  1384. Revision 1.116 2003/08/17 16:59:20 jonas
  1385. * fixed regvars so they work with newra (at least for ppc)
  1386. * fixed some volatile register bugs
  1387. + -dnotranslation option for -dnewra, which causes the registers not to
  1388. be translated from virtual to normal registers. Requires support in
  1389. the assembler writer as well, which is only implemented in aggas/
  1390. agppcgas currently
  1391. Revision 1.115 2003/07/23 11:01:14 jonas
  1392. * several rg.allocexplicitregistersint/rg.deallocexplicitregistersint
  1393. pairs round calls to helpers
  1394. Revision 1.114 2003/07/06 17:58:22 peter
  1395. * framepointer fixes for sparc
  1396. * parent framepointer code more generic
  1397. Revision 1.113 2003/07/02 22:18:04 peter
  1398. * paraloc splitted in callerparaloc,calleeparaloc
  1399. * sparc calling convention updates
  1400. Revision 1.112 2003/06/13 21:19:30 peter
  1401. * current_procdef removed, use current_procinfo.procdef instead
  1402. Revision 1.111 2003/06/12 21:11:10 peter
  1403. * ungetregisterfpu gets size parameter
  1404. Revision 1.110 2003/06/12 16:43:07 peter
  1405. * newra compiles for sparc
  1406. Revision 1.109 2003/06/07 18:57:04 jonas
  1407. + added freeintparaloc
  1408. * ppc get/freeintparaloc now check whether the parameter regs are
  1409. properly allocated/deallocated (and get an extra list para)
  1410. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1411. * fixed lot of missing pi_do_call's
  1412. Revision 1.108 2003/06/06 14:43:02 peter
  1413. * g_copyopenarrayvalue gets length reference
  1414. * don't copy open arrays for cdecl
  1415. Revision 1.107 2003/06/03 21:11:09 peter
  1416. * cg.a_load_* get a from and to size specifier
  1417. * makeregsize only accepts newregister
  1418. * i386 uses generic tcgnotnode,tcgunaryminus
  1419. Revision 1.106 2003/06/03 13:01:59 daniel
  1420. * Register allocator finished
  1421. Revision 1.105 2003/06/01 21:38:06 peter
  1422. * getregisterfpu size parameter added
  1423. * op_const_reg size parameter added
  1424. * sparc updates
  1425. Revision 1.104 2003/06/01 01:02:39 peter
  1426. * generic a_call_ref
  1427. Revision 1.103 2003/05/30 23:57:08 peter
  1428. * more sparc cleanup
  1429. * accumulator removed, splitted in function_return_reg (called) and
  1430. function_result_reg (caller)
  1431. Revision 1.102 2003/05/30 23:49:18 jonas
  1432. * a_load_loc_reg now has an extra size parameter for the destination
  1433. register (properly fixes what I worked around in revision 1.106 of
  1434. ncgutil.pas)
  1435. Revision 1.101 2003/05/30 21:40:00 jonas
  1436. * fixed bug in a_load_loc_ref (the source instead of dest size was passed
  1437. to a_load_reg_ref in case of a register)
  1438. Revision 1.100 2003/05/30 12:36:13 jonas
  1439. * use as little different registers on the ppc until newra is released,
  1440. since every used register must be saved
  1441. Revision 1.99 2003/05/23 14:27:35 peter
  1442. * remove some unit dependencies
  1443. * current_procinfo changes to store more info
  1444. Revision 1.98 2003/05/15 18:58:53 peter
  1445. * removed selfpointer_offset, vmtpointer_offset
  1446. * tvarsym.adjusted_address
  1447. * address in localsymtable is now in the real direction
  1448. * removed some obsolete globals
  1449. Revision 1.97 2003/05/13 19:14:41 peter
  1450. * failn removed
  1451. * inherited result code check moven to pexpr
  1452. Revision 1.96 2003/05/11 21:37:03 peter
  1453. * moved implicit exception frame from ncgutil to psub
  1454. * constructor/destructor helpers moved from cobj/ncgutil to psub
  1455. Revision 1.95 2003/05/09 17:47:02 peter
  1456. * self moved to hidden parameter
  1457. * removed hdisposen,hnewn,selfn
  1458. Revision 1.94 2003/05/01 12:23:46 jonas
  1459. * fix for op_reg_reg_reg in case the destination is the same as the first
  1460. source register
  1461. Revision 1.93 2003/04/29 07:28:52 michael
  1462. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1463. Revision 1.92 2003/04/27 11:21:32 peter
  1464. * aktprocdef renamed to current_procinfo.procdef
  1465. * procinfo renamed to current_procinfo
  1466. * procinfo will now be stored in current_module so it can be
  1467. cleaned up properly
  1468. * gen_main_procsym changed to create_main_proc and release_main_proc
  1469. to also generate a tprocinfo structure
  1470. * fixed unit implicit initfinal
  1471. Revision 1.91 2003/04/27 07:29:50 peter
  1472. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1473. a new procdef declaration
  1474. * aktprocsym removed
  1475. * lexlevel removed, use symtable.symtablelevel instead
  1476. * implicit init/final code uses the normal genentry/genexit
  1477. * funcret state checking updated for new funcret handling
  1478. Revision 1.90 2003/04/26 20:57:17 florian
  1479. * fixed para locations of fpc_class_new helper call
  1480. Revision 1.89 2003/04/26 17:21:08 florian
  1481. * fixed passing of fpu values by fpu register
  1482. Revision 1.88 2003/04/23 20:16:03 peter
  1483. + added currency support based on int64
  1484. + is_64bit for use in cg units instead of is_64bitint
  1485. * removed cgmessage from n386add, replace with internalerrors
  1486. Revision 1.87 2003/04/23 14:42:07 daniel
  1487. * Further register allocator work. Compiler now smaller with new
  1488. allocator than without.
  1489. * Somebody forgot to adjust ppu version number
  1490. Revision 1.86 2003/04/23 13:20:34 peter
  1491. * fix self passing to fpc_help_fail
  1492. Revision 1.85 2003/04/23 12:35:34 florian
  1493. * fixed several issues with powerpc
  1494. + applied a patch from Jonas for nested function calls (PowerPC only)
  1495. * ...
  1496. Revision 1.84 2003/04/22 14:33:38 peter
  1497. * removed some notes/hints
  1498. Revision 1.83 2003/04/22 13:47:08 peter
  1499. * fixed C style array of const
  1500. * fixed C array passing
  1501. * fixed left to right with high parameters
  1502. Revision 1.82 2003/04/22 10:09:34 daniel
  1503. + Implemented the actual register allocator
  1504. + Scratch registers unavailable when new register allocator used
  1505. + maybe_save/maybe_restore unavailable when new register allocator used
  1506. Revision 1.81 2003/04/06 21:11:23 olle
  1507. * changed newasmsymbol to newasmsymboldata for data symbols
  1508. Revision 1.80 2003/03/28 19:16:56 peter
  1509. * generic constructor working for i386
  1510. * remove fixed self register
  1511. * esi added as address register for i386
  1512. Revision 1.79 2003/03/22 18:07:18 jonas
  1513. + add used scratch registers to usedintbyproc for non-i386
  1514. Revision 1.78 2003/03/11 21:46:24 jonas
  1515. * lots of new regallocator fixes, both in generic and ppc-specific code
  1516. (ppc compiler still can't compile the linux system unit though)
  1517. Revision 1.77 2003/02/19 22:00:14 daniel
  1518. * Code generator converted to new register notation
  1519. - Horribily outdated todo.txt removed
  1520. Revision 1.76 2003/01/31 22:47:48 peter
  1521. * maybe_testself now really uses the passed register
  1522. Revision 1.75 2003/01/30 21:46:35 peter
  1523. * maybe_testvmt added
  1524. Revision 1.74 2003/01/17 12:45:40 daniel
  1525. * Fixed internalerror 200301081 problem
  1526. Revision 1.73 2003/01/13 14:54:34 daniel
  1527. * Further work to convert codegenerator register convention;
  1528. internalerror bug fixed.
  1529. Revision 1.72 2003/01/09 22:00:53 florian
  1530. * fixed some PowerPC issues
  1531. Revision 1.71 2003/01/09 20:41:10 florian
  1532. * fixed broken PowerPC compiler
  1533. Revision 1.70 2003/01/08 18:43:56 daniel
  1534. * Tregister changed into a record
  1535. Revision 1.69 2002/12/24 15:56:50 peter
  1536. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1537. this for the pageprotection
  1538. Revision 1.68 2002/12/20 18:14:04 peter
  1539. * removed some runerror and writeln
  1540. Revision 1.67 2002/12/14 15:02:03 carl
  1541. * maxoperands -> max_operands (for portability in rautils.pas)
  1542. * fix some range-check errors with loadconst
  1543. + add ncgadd unit to m68k
  1544. * some bugfix of a_param_reg with LOC_CREFERENCE
  1545. Revision 1.66 2002/11/25 17:43:16 peter
  1546. * splitted defbase in defutil,symutil,defcmp
  1547. * merged isconvertable and is_equal into compare_defs(_ext)
  1548. * made operator search faster by walking the list only once
  1549. Revision 1.65 2002/11/17 16:27:31 carl
  1550. * document flags2reg
  1551. Revision 1.64 2002/11/16 17:06:28 peter
  1552. * return error 210 for failed self test
  1553. Revision 1.63 2002/11/15 01:58:46 peter
  1554. * merged changes from 1.0.7 up to 04-11
  1555. - -V option for generating bug report tracing
  1556. - more tracing for option parsing
  1557. - errors for cdecl and high()
  1558. - win32 import stabs
  1559. - win32 records<=8 are returned in eax:edx (turned off by default)
  1560. - heaptrc update
  1561. - more info for temp management in .s file with EXTDEBUG
  1562. Revision 1.62 2002/10/16 19:01:43 peter
  1563. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1564. implicit exception frames for procedures with initialized variables
  1565. and for constructors. The default is on for compatibility
  1566. Revision 1.61 2002/10/05 12:43:23 carl
  1567. * fixes for Delphi 6 compilation
  1568. (warning : Some features do not work under Delphi)
  1569. Revision 1.60 2002/10/02 18:20:52 peter
  1570. * Copy() is now internal syssym that calls compilerprocs
  1571. Revision 1.59 2002/09/17 18:54:02 jonas
  1572. * a_load_reg_reg() now has two size parameters: source and dest. This
  1573. allows some optimizations on architectures that don't encode the
  1574. register size in the register name.
  1575. Revision 1.58 2002/09/09 19:29:29 peter
  1576. * fixed dynarr_decr_ref call
  1577. Revision 1.57 2002/09/07 15:25:01 peter
  1578. * old logs removed and tabs fixed
  1579. Revision 1.56 2002/09/01 21:04:47 florian
  1580. * several powerpc related stuff fixed
  1581. Revision 1.55 2002/09/01 17:05:43 florian
  1582. + added abstract tcg.g_removevaluepara_openarray
  1583. Revision 1.54 2002/09/01 12:09:27 peter
  1584. + a_call_reg, a_call_loc added
  1585. * removed exprasmlist references
  1586. Revision 1.53 2002/08/19 18:17:48 carl
  1587. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  1588. * more fixes to m68k for 64-bit operations
  1589. Revision 1.52 2002/08/17 22:09:43 florian
  1590. * result type handling in tcgcal.pass_2 overhauled
  1591. * better tnode.dowrite
  1592. * some ppc stuff fixed
  1593. Revision 1.51 2002/08/17 09:23:33 florian
  1594. * first part of procinfo rewrite
  1595. Revision 1.50 2002/08/16 14:24:57 carl
  1596. * issameref() to test if two references are the same (then emit no opcodes)
  1597. + ret_in_reg to replace ret_in_acc
  1598. (fix some register allocation bugs at the same time)
  1599. + save_std_register now has an extra parameter which is the
  1600. usedinproc registers
  1601. Revision 1.49 2002/08/15 08:13:54 carl
  1602. - a_load_sym_ofs_reg removed
  1603. * loadvmt now calls loadaddr_ref_reg instead
  1604. Revision 1.48 2002/08/14 19:26:02 carl
  1605. + routine to optimize opcodes with constants
  1606. Revision 1.47 2002/08/11 14:32:26 peter
  1607. * renamed current_library to objectlibrary
  1608. Revision 1.46 2002/08/11 13:24:11 peter
  1609. * saving of asmsymbols in ppu supported
  1610. * asmsymbollist global is removed and moved into a new class
  1611. tasmlibrarydata that will hold the info of a .a file which
  1612. corresponds with a single module. Added librarydata to tmodule
  1613. to keep the library info stored for the module. In the future the
  1614. objectfiles will also be stored to the tasmlibrarydata class
  1615. * all getlabel/newasmsymbol and friends are moved to the new class
  1616. Revision 1.45 2002/08/10 17:15:20 jonas
  1617. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  1618. Revision 1.44 2002/08/09 19:10:05 carl
  1619. - moved new_exception and free_exception to ncgutils
  1620. Revision 1.43 2002/08/05 18:27:48 carl
  1621. + more more more documentation
  1622. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1623. Revision 1.42 2002/08/04 19:08:21 carl
  1624. + added generic exception support (still does not work!)
  1625. + more documentation
  1626. Revision 1.41 2002/07/30 20:50:43 florian
  1627. * the code generator knows now if parameters are in registers
  1628. Revision 1.40 2002/07/29 21:16:02 florian
  1629. * some more ppc fixes
  1630. Revision 1.39 2002/07/28 15:56:00 jonas
  1631. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1632. generic implementation
  1633. Revision 1.38 2002/07/27 19:53:51 jonas
  1634. + generic implementation of tcg.g_flags2ref()
  1635. * tcg.flags2xxx() now also needs a size parameter
  1636. Revision 1.37 2002/07/20 11:57:53 florian
  1637. * types.pas renamed to defbase.pas because D6 contains a types
  1638. unit so this would conflicts if D6 programms are compiled
  1639. + Willamette/SSE2 instructions to assembler added
  1640. }