cgobj.pas 81 KB

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