cgobj.pas 78 KB

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