cgobj.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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. }
  23. unit cgobj;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cclasses,aasmbase,aasmtai,aasmcpu,symtable,
  28. cpubase,cpuinfo,cpupara,
  29. cginfo,
  30. symconst,symbase,symtype,node;
  31. type
  32. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  33. {# @abstract(Abstract code generator)
  34. This class implements an abstract instruction generator. Some of
  35. the methods of this class are generic, while others must
  36. be overriden for all new processors which will be supported
  37. by Free Pascal. For 32-bit processors, the base class
  38. sould be @link(tcg64f32) and not @var(tcg).
  39. }
  40. tcg = class
  41. scratch_register_array_pointer : aword;
  42. {# List of currently unused scratch registers }
  43. unusedscratchregisters : tregisterset;
  44. alignment : talignment;
  45. {************************************************}
  46. { basic routines }
  47. constructor create;
  48. { returns the tcgsize corresponding with the size of reg }
  49. class function reg_cgsize(const reg: tregister) : tcgsize; virtual;
  50. {# Emit a label to the instruction stream. }
  51. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  52. {# Allocates register r by inserting a pai_realloc record }
  53. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  54. {# Deallocates register r by inserting a pa_regdealloc record}
  55. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  56. {# @abstract(Returns an int register for use as scratch register)
  57. This routine returns a register which can be used by
  58. the code generator as a general purpose scratch register.
  59. Since scratch_registers are scarce resources, the register
  60. should be freed by calling @link(free_scratch_reg) as
  61. soon as it is no longer required.
  62. }
  63. function get_scratch_reg_int(list : taasmoutput) : tregister;virtual;
  64. {# @abstract(Returns an address register for use as scratch register)
  65. This routine returns a register which can be used by
  66. the code generator as a pointer scratch register.
  67. Since scratch_registers are scarce resources, the register
  68. should be freed by calling @link(free_scratch_reg) as
  69. soon as it is no longer required.
  70. }
  71. function get_scratch_reg_address(list : taasmoutput) : tregister;virtual;
  72. {# @abstract(Releases a scratch register)
  73. Releases a scratch register.
  74. This routine is used to free a register which
  75. was previously allocated using @link(get_scratch_reg).
  76. }
  77. procedure free_scratch_reg(list : taasmoutput;r : tregister);
  78. { passing parameters, per default the parameter is pushed }
  79. { nr gives the number of the parameter (enumerated from }
  80. { left to right), this allows to move the parameter to }
  81. { register, if the cpu supports register calling }
  82. { conventions }
  83. {# Pass a parameter, which is located in a register, to a routine.
  84. This routine should push/send the parameter to the routine, as
  85. required by the specific processor ABI. This must be overriden for
  86. each CPU target.
  87. @param(size size of the operand in the register)
  88. @param(r register source of the operand)
  89. @param(nr parameter number (starting from one) of routine (from left to right))
  90. }
  91. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);virtual;
  92. {# Pass a parameter, which is a constant, to a routine.
  93. A generic version is provided.
  94. @param(size size of the operand in constant)
  95. @param(a value of constant to send)
  96. @param(nr parameter number (starting from one) of routine (from left to right))
  97. }
  98. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);virtual;
  99. {# Pass the value of a parameter, which is located in memory, to a routine.
  100. A generic version is provided.
  101. @param(size size of the operand in constant)
  102. @param(r Memory reference of value to send)
  103. @param(nr parameter number (starting from one) of routine (from left to right))
  104. }
  105. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);virtual;
  106. {# Pass the value of a parameter, which can be located either in a register or memory location,
  107. to a routine.
  108. A generic version is provided.
  109. @param(l location of the operand to send)
  110. @param(nr parameter number (starting from one) of routine (from left to right))
  111. }
  112. procedure a_param_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);
  113. {# Pass the address of a reference to a routine.
  114. A generic version is provided.
  115. @param(r reference to get address from)
  116. @param(nr parameter number (starting from one) of routine (from left to right))
  117. }
  118. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);virtual;
  119. {**********************************}
  120. { these methods must be overriden: }
  121. { Remarks:
  122. * If a method specifies a size you have only to take care
  123. of that number of bits, i.e. load_const_reg with OP_8 must
  124. only load the lower 8 bit of the specified register
  125. the rest of the register can be undefined
  126. if necessary the compiler will call a method
  127. to zero or sign extend the register
  128. * The a_load_XX_XX with OP_64 needn't to be
  129. implemented for 32 bit
  130. processors, the code generator takes care of that
  131. * the addr size is for work with the natural pointer
  132. size
  133. * the procedures without fpu/mm are only for integer usage
  134. * normally the first location is the source and the
  135. second the destination
  136. }
  137. {# Emits instruction to call the method specified by symbol name.
  138. This routine must be overriden for each new target cpu.
  139. }
  140. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  141. procedure a_call_ref(list : taasmoutput;const ref : treference);virtual; abstract;
  142. { move instructions }
  143. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  144. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  145. procedure a_load_const_loc(list : taasmoutput;a : aword;const loc : tlocation);
  146. procedure a_load_reg_ref(list : taasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  147. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  148. procedure a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  149. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  150. procedure a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);virtual;
  151. procedure a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  152. procedure a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  153. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);virtual; abstract;
  154. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  155. { fpu move instructions }
  156. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  157. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  158. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  159. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  160. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  161. { vector register move instructions }
  162. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); virtual; abstract;
  163. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); virtual; abstract;
  164. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); virtual; abstract;
  165. procedure a_parammm_reg(list: taasmoutput; reg: tregister); virtual; abstract;
  166. { basic arithmetic operations }
  167. { note: for operators which require only one argument (not, neg), use }
  168. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  169. { that in this case the *second* operand is used as both source and }
  170. { destination (JM) }
  171. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  172. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  173. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  174. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  175. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  176. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  177. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  178. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  179. { trinary operations for processors that support them, 'emulated' }
  180. { on others. None with "ref" arguments since I don't think there }
  181. { are any processors that support it (JM) }
  182. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  183. size: tcgsize; a: aword; src, dst: tregister); virtual;
  184. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  185. size: tcgsize; src1, src2, dst: tregister); virtual;
  186. { comparison operations }
  187. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  188. l : tasmlabel);virtual; abstract;
  189. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  190. l : tasmlabel); virtual;
  191. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  192. l : tasmlabel);
  193. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  194. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  195. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  196. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  197. l : tasmlabel);
  198. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  199. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  200. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  201. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  202. { some processors like the PPC doesn't allow to change the stack in }
  203. { a procedure, so we need to maintain an extra stack for the }
  204. { result values of setjmp in exception code }
  205. { this two procedures are for pushing an exception value, }
  206. { they can use the scratch registers }
  207. procedure g_push_exception(list : taasmoutput;const exceptbuf:treference;l:AWord; exceptlabel:TAsmLabel);virtual;abstract;
  208. procedure g_pop_exception(list : taasmoutput;endexceptlabel:tasmlabel);virtual;abstract;
  209. procedure g_maybe_loadself(list : taasmoutput);virtual;
  210. {# This should emit the opcode to copy len bytes from the source
  211. to destination, if loadref is true, it assumes that it first must load
  212. the source address from the memory location where
  213. source points to.
  214. It must be overriden for each new target processor.
  215. @param(source Source reference of copy)
  216. @param(dest Destination reference of copy)
  217. @param(delsource Indicates if the source reference's resources should be freed)
  218. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  219. }
  220. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  221. {# This should emit the opcode to a shortrstring from the source
  222. to destination, if loadref is true, it assumes that it first must load
  223. the source address from the memory location where
  224. source points to.
  225. @param(source Source reference of copy)
  226. @param(dest Destination reference of copy)
  227. @param(delsource Indicates if the source reference's resources should be freed)
  228. @param(loadref Is the source reference a pointer to the actual source (TRUE), is it the actual source address (FALSE))
  229. }
  230. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  231. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  232. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  233. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  234. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  235. {# Emits the call to the stack checking routine of
  236. the runtime library. The default behavior
  237. does not need to be modified, as it is generic
  238. for all platforms.
  239. }
  240. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  241. {# Generates range checking code. It is to note
  242. that this routine does not need to be overriden,
  243. as it takes care of everything.
  244. @param(p Node which contains the value to check)
  245. @param(todef Type definition of node to range check)
  246. }
  247. procedure g_rangecheck(list: taasmoutput; const p: tnode;
  248. const todef: tdef); virtual;
  249. { generates overflow checking code for a node }
  250. procedure g_overflowcheck(list: taasmoutput; const p: tnode); virtual; abstract;
  251. {**********************************}
  252. { entry/exit code helpers }
  253. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer); virtual; abstract;
  254. {# Emits instructions which should be emitted when entering
  255. a routine declared as @var(interrupt). The default
  256. behavior does nothing, should be overriden as required.
  257. }
  258. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  259. {# Emits instructions which should be emitted when exiting
  260. a routine declared as @var(interrupt). The default
  261. behavior does nothing, should be overriden as required.
  262. }
  263. procedure g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;
  264. {# Emits instructions when compilation is done in profile
  265. mode (this is set as a command line option). The default
  266. behavior does nothing, should be overriden as required.
  267. }
  268. procedure g_profilecode(list : taasmoutput);virtual;
  269. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  270. { restores the frame pointer at procedure exit }
  271. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  272. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  273. procedure g_call_constructor_helper(list : taasmoutput);virtual;
  274. procedure g_call_destructor_helper(list : taasmoutput);virtual;
  275. procedure g_call_fail_helper(list : taasmoutput);virtual;
  276. procedure g_save_standard_registers(list : taasmoutput);virtual;abstract;
  277. procedure g_restore_standard_registers(list : taasmoutput);virtual;abstract;
  278. procedure g_save_all_registers(list : taasmoutput);virtual;abstract;
  279. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);virtual;abstract;
  280. end;
  281. {# @abstract(Abstract code generator for 64 Bit operations)
  282. This class implements an abstract code generator class
  283. for 64 Bit operations.
  284. }
  285. tcg64 = class
  286. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);virtual;abstract;
  287. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  288. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  289. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  290. procedure a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);virtual;abstract;
  291. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  292. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  293. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);virtual;abstract;
  294. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  295. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  296. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  297. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  298. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  299. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  300. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  301. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);virtual;abstract;
  302. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);virtual;abstract;
  303. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;regsrc : tregister64;const ref : treference);virtual;abstract;
  304. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;regdst : tregister64);virtual;abstract;
  305. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);virtual;abstract;
  306. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);virtual;abstract;
  307. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);virtual;abstract;
  308. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg64 : tregister64);virtual;abstract;
  309. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);virtual;
  310. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);virtual;
  311. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  312. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  313. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  314. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  315. { override to catch 64bit rangechecks }
  316. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  317. const todef: tdef);virtual;abstract;
  318. end;
  319. var
  320. {# Main code generator class }
  321. cg : tcg;
  322. {# Code generator class for all operations working with 64-Bit operands }
  323. cg64 : tcg64;
  324. implementation
  325. uses
  326. globals,globtype,options,systems,cgbase,
  327. verbose,defbase,tgobj,symdef,paramgr,
  328. rgobj;
  329. const
  330. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  331. {*****************************************************************************
  332. basic functionallity
  333. ******************************************************************************}
  334. constructor tcg.create;
  335. var
  336. i : longint;
  337. begin
  338. scratch_register_array_pointer:=1;
  339. for i:=low(scratch_regs) to high(scratch_regs) do
  340. include(unusedscratchregisters,scratch_regs[i]);
  341. end;
  342. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  343. begin
  344. list.concat(tai_regalloc.alloc(r));
  345. end;
  346. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  347. begin
  348. list.concat(tai_regalloc.dealloc(r));
  349. end;
  350. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  351. begin
  352. list.concat(tai_label.create(l));
  353. end;
  354. function tcg.get_scratch_reg_int(list : taasmoutput) : tregister;
  355. var
  356. r : tregister;
  357. i : longint;
  358. begin
  359. if unusedscratchregisters=[] then
  360. internalerror(68996);
  361. for i:=scratch_register_array_pointer to
  362. (scratch_register_array_pointer+max_scratch_regs-1) do
  363. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  364. begin
  365. r:=scratch_regs[(i mod max_scratch_regs)+1];
  366. break;
  367. end;
  368. exclude(unusedscratchregisters,r);
  369. inc(scratch_register_array_pointer);
  370. if scratch_register_array_pointer>max_scratch_regs then
  371. scratch_register_array_pointer:=1;
  372. a_reg_alloc(list,r);
  373. get_scratch_reg_int:=r;
  374. end;
  375. { the default behavior simply returns a general purpose register }
  376. function tcg.get_scratch_reg_address(list : taasmoutput) : tregister;
  377. begin
  378. get_scratch_reg_address := get_scratch_reg_int(list);
  379. end;
  380. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  381. begin
  382. include(unusedscratchregisters,rg.makeregsize(r,OS_INT));
  383. a_reg_dealloc(list,r);
  384. end;
  385. {*****************************************************************************
  386. for better code generation these methods should be overridden
  387. ******************************************************************************}
  388. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  389. var
  390. ref : treference;
  391. begin
  392. case locpara.loc of
  393. LOC_REGISTER:
  394. a_load_reg_reg(list,size,r,locpara.register);
  395. LOC_REFERENCE:
  396. begin
  397. reference_reset(ref);
  398. ref.base:=locpara.reference.index;
  399. ref.offset:=locpara.reference.offset;
  400. a_load_reg_ref(list,size,r,ref);
  401. {!!!! FIX ME!, take sp_fixup into account }
  402. internalerror(2002071005);
  403. end
  404. else
  405. internalerror(2002071004);
  406. end;
  407. end;
  408. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  409. var
  410. hr : tregister;
  411. begin
  412. hr:=get_scratch_reg_int(list);
  413. a_load_const_reg(list,size,a,hr);
  414. a_param_reg(list,size,hr,locpara);
  415. free_scratch_reg(list,hr);
  416. end;
  417. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  418. var
  419. hr : tregister;
  420. begin
  421. hr:=get_scratch_reg_int(list);
  422. a_load_ref_reg(list,size,r,hr);
  423. a_param_reg(list,size,hr,locpara);
  424. free_scratch_reg(list,hr);
  425. end;
  426. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  427. begin
  428. case l.loc of
  429. LOC_REGISTER,
  430. LOC_CREGISTER :
  431. a_param_reg(list,l.size,l.register,locpara);
  432. LOC_CONSTANT :
  433. a_param_const(list,l.size,l.value,locpara);
  434. LOC_CREFERENCE,
  435. LOC_REFERENCE :
  436. a_param_ref(list,l.size,l.reference,locpara);
  437. else
  438. internalerror(2002032211);
  439. end;
  440. end;
  441. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  442. var
  443. hr : tregister;
  444. begin
  445. hr:=get_scratch_reg_address(list);
  446. a_loadaddr_ref_reg(list,r,hr);
  447. a_param_reg(list,OS_ADDR,hr,locpara);
  448. free_scratch_reg(list,hr);
  449. end;
  450. {****************************************************************************
  451. some generic implementations
  452. ****************************************************************************}
  453. procedure tcg.a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);
  454. var
  455. tmpreg: tregister;
  456. {$ifdef i386}
  457. pushed_reg: tregister;
  458. {$endif i386}
  459. begin
  460. {$ifdef i386}
  461. { the following is done with defines to avoid a speed penalty, }
  462. { since all this is only necessary for the 80x86 (because EDI }
  463. { doesn't have an 8bit component which is directly addressable) }
  464. pushed_reg := R_NO;
  465. if size in [OS_8,OS_S8] then
  466. if (rg.countunusedregsint = 0) then
  467. begin
  468. if (dref.base <> R_EBX) and
  469. (dref.index <> R_EBX) then
  470. pushed_reg := R_EBX
  471. else if (dref.base <> R_EAX) and
  472. (dref.index <> R_EAX) then
  473. pushed_reg := R_EAX
  474. else pushed_reg := R_ECX;
  475. tmpreg := rg.makeregsize(pushed_reg,OS_8);
  476. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  477. end
  478. else
  479. tmpreg := rg.getregisterint(exprasmlist)
  480. else
  481. {$endif i386}
  482. tmpreg := get_scratch_reg_int(list);
  483. tmpreg:=rg.makeregsize(tmpreg,size);
  484. a_load_ref_reg(list,size,sref,tmpreg);
  485. a_load_reg_ref(list,size,tmpreg,dref);
  486. {$ifdef i386}
  487. if size in [OS_8,OS_S8] then
  488. begin
  489. if (pushed_reg <> R_NO) then
  490. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  491. else
  492. rg.ungetregister(exprasmlist,tmpreg)
  493. end
  494. else
  495. {$endif i386}
  496. free_scratch_reg(list,tmpreg);
  497. end;
  498. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  499. var
  500. tmpreg: tregister;
  501. begin
  502. tmpreg := get_scratch_reg_int(list);
  503. a_load_const_reg(list,size,a,tmpreg);
  504. a_load_reg_ref(list,size,tmpreg,ref);
  505. free_scratch_reg(list,tmpreg);
  506. end;
  507. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  508. begin
  509. case loc.loc of
  510. LOC_REFERENCE,LOC_CREFERENCE:
  511. a_load_const_ref(list,loc.size,a,loc.reference);
  512. LOC_REGISTER,LOC_CREGISTER:
  513. a_load_const_reg(list,loc.size,a,loc.register);
  514. else
  515. internalerror(200203272);
  516. end;
  517. end;
  518. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  519. begin
  520. case loc.loc of
  521. LOC_REFERENCE,LOC_CREFERENCE:
  522. a_load_reg_ref(list,size,reg,loc.reference);
  523. LOC_REGISTER,LOC_CREGISTER:
  524. a_load_reg_reg(list,size,reg,loc.register);
  525. else
  526. internalerror(200203271);
  527. end;
  528. end;
  529. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  530. begin
  531. case loc.loc of
  532. LOC_REFERENCE,LOC_CREFERENCE:
  533. a_load_ref_reg(list,loc.size,loc.reference,reg);
  534. LOC_REGISTER,LOC_CREGISTER:
  535. a_load_reg_reg(list,loc.size,loc.register,reg);
  536. LOC_CONSTANT:
  537. a_load_const_reg(list,loc.size,loc.value,reg);
  538. else
  539. internalerror(200109092);
  540. end;
  541. end;
  542. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  543. begin
  544. case loc.loc of
  545. LOC_REFERENCE,LOC_CREFERENCE:
  546. a_load_ref_ref(list,loc.size,loc.reference,ref);
  547. LOC_REGISTER,LOC_CREGISTER:
  548. a_load_reg_ref(list,loc.size,loc.register,ref);
  549. LOC_CONSTANT:
  550. a_load_const_ref(list,loc.size,loc.value,ref);
  551. else
  552. internalerror(200109302);
  553. end;
  554. end;
  555. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  556. begin
  557. case loc.loc of
  558. LOC_REFERENCE, LOC_CREFERENCE:
  559. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  560. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  561. a_loadfpu_reg_reg(list,loc.register,reg);
  562. else
  563. internalerror(200203301);
  564. end;
  565. end;
  566. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  567. begin
  568. case loc.loc of
  569. LOC_REFERENCE, LOC_CREFERENCE:
  570. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  571. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  572. a_loadfpu_reg_reg(list,reg,loc.register);
  573. else
  574. internalerror(48991);
  575. end;
  576. end;
  577. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  578. var
  579. tmpreg: tregister;
  580. begin
  581. tmpreg := get_scratch_reg_int(list);
  582. a_load_ref_reg(list,size,ref,tmpreg);
  583. a_op_const_reg(list,op,a,tmpreg);
  584. a_load_reg_ref(list,size,tmpreg,ref);
  585. free_scratch_reg(list,tmpreg);
  586. end;
  587. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  588. begin
  589. case loc.loc of
  590. LOC_REGISTER, LOC_CREGISTER:
  591. a_op_const_reg(list,op,a,loc.register);
  592. LOC_REFERENCE, LOC_CREFERENCE:
  593. a_op_const_ref(list,op,loc.size,a,loc.reference);
  594. else
  595. internalerror(200109061);
  596. end;
  597. end;
  598. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  599. var
  600. tmpreg: tregister;
  601. begin
  602. tmpreg := get_scratch_reg_int(list);
  603. a_load_ref_reg(list,size,ref,tmpreg);
  604. a_op_reg_reg(list,op,size,reg,tmpreg);
  605. a_load_reg_ref(list,size,tmpreg,ref);
  606. free_scratch_reg(list,tmpreg);
  607. end;
  608. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  609. var
  610. tmpreg: tregister;
  611. begin
  612. case op of
  613. OP_NOT,OP_NEG:
  614. { handle it as "load ref,reg; op reg" }
  615. begin
  616. a_load_ref_reg(list,size,ref,reg);
  617. a_op_reg_reg(list,op,size,reg,reg);
  618. end;
  619. else
  620. begin
  621. tmpreg := get_scratch_reg_int(list);
  622. a_load_ref_reg(list,size,ref,tmpreg);
  623. a_op_reg_reg(list,op,size,tmpreg,reg);
  624. free_scratch_reg(list,tmpreg);
  625. end;
  626. end;
  627. end;
  628. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  629. begin
  630. case loc.loc of
  631. LOC_REGISTER, LOC_CREGISTER:
  632. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  633. LOC_REFERENCE, LOC_CREFERENCE:
  634. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  635. else
  636. internalerror(200109061);
  637. end;
  638. end;
  639. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  640. var
  641. tmpreg: tregister;
  642. begin
  643. case loc.loc of
  644. LOC_REGISTER,LOC_CREGISTER:
  645. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  646. LOC_REFERENCE,LOC_CREFERENCE:
  647. begin
  648. tmpreg := get_scratch_reg_int(list);
  649. tmpreg:=rg.makeregsize(tmpreg,loc.size);
  650. a_load_ref_reg(list,loc.size,ref,tmpreg);
  651. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  652. free_scratch_reg(list,tmpreg);
  653. end;
  654. else
  655. internalerror(200109061);
  656. end;
  657. end;
  658. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  659. size: tcgsize; a: aword; src, dst: tregister);
  660. begin
  661. a_load_reg_reg(list,size,src,dst);
  662. a_op_const_reg(list,op,a,dst);
  663. end;
  664. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  665. size: tcgsize; src1, src2, dst: tregister);
  666. begin
  667. a_load_reg_reg(list,size,src2,dst);
  668. a_op_reg_reg(list,op,size,src1,dst);
  669. end;
  670. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  671. l : tasmlabel);
  672. var
  673. tmpreg: tregister;
  674. begin
  675. tmpreg := get_scratch_reg_int(list);
  676. a_load_ref_reg(list,size,ref,tmpreg);
  677. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  678. free_scratch_reg(list,tmpreg);
  679. end;
  680. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  681. l : tasmlabel);
  682. begin
  683. case loc.loc of
  684. LOC_REGISTER,LOC_CREGISTER:
  685. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  686. LOC_REFERENCE,LOC_CREFERENCE:
  687. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  688. else
  689. internalerror(200109061);
  690. end;
  691. end;
  692. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  693. var
  694. tmpreg: tregister;
  695. begin
  696. tmpreg := get_scratch_reg_int(list);
  697. a_load_ref_reg(list,size,ref,tmpreg);
  698. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  699. free_scratch_reg(list,tmpreg);
  700. end;
  701. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  702. begin
  703. case loc.loc of
  704. LOC_REGISTER,
  705. LOC_CREGISTER:
  706. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  707. LOC_REFERENCE,
  708. LOC_CREFERENCE :
  709. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  710. LOC_CONSTANT:
  711. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  712. else
  713. internalerror(200203231);
  714. end;
  715. end;
  716. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  717. l : tasmlabel);
  718. var
  719. tmpreg: tregister;
  720. begin
  721. case loc.loc of
  722. LOC_REGISTER,LOC_CREGISTER:
  723. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  724. LOC_REFERENCE,LOC_CREFERENCE:
  725. begin
  726. {$ifdef i386}
  727. { the following is done with defines to avoid a speed penalty, }
  728. { since all this is only necessary for the 80x86 (because EDI }
  729. { doesn't have an 8bit component which is directly addressable) }
  730. if size in [OS_8,OS_S8] then
  731. tmpreg := rg.getregisterint(exprasmlist)
  732. else
  733. {$endif i386}
  734. tmpreg := get_scratch_reg_int(list);
  735. tmpreg := rg.makeregsize(tmpreg,size);
  736. a_load_ref_reg(list,size,loc.reference,tmpreg);
  737. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  738. {$ifdef i386}
  739. if size in [OS_8,OS_S8] then
  740. rg.ungetregister(exprasmlist,tmpreg)
  741. else
  742. {$endif i386}
  743. free_scratch_reg(list,tmpreg);
  744. end
  745. else
  746. internalerror(200109061);
  747. end;
  748. end;
  749. function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  750. begin
  751. reg_cgsize := OS_INT;
  752. end;
  753. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  754. begin
  755. {$warning FIX ME!}
  756. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  757. if loadref then
  758. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  759. else
  760. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  761. if delsource then
  762. reference_release(list,source);
  763. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  764. a_call_name(list,'FPC_SHORTSTR_COPY');
  765. g_maybe_loadself(list);
  766. end;
  767. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  768. var
  769. href : treference;
  770. incrfunc : string;
  771. begin
  772. { These functions should not change the registers (they use
  773. the saveregister proc directive }
  774. if is_interfacecom(t) then
  775. incrfunc:='FPC_INTF_INCR_REF'
  776. else if is_ansistring(t) then
  777. incrfunc:='FPC_ANSISTR_INCR_REF'
  778. else if is_widestring(t) then
  779. incrfunc:='FPC_WIDESTR_INCR_REF'
  780. else if is_dynamic_array(t) then
  781. incrfunc:='FPC_DYNARRAY_INCR_REF'
  782. else
  783. incrfunc:='';
  784. { call the special incr function or the generic addref }
  785. if incrfunc<>'' then
  786. begin
  787. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  788. a_call_name(list,incrfunc);
  789. end
  790. else
  791. begin
  792. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  793. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  794. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  795. a_call_name(list,'FPC_ADDREF');
  796. end;
  797. end;
  798. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  799. var
  800. href : treference;
  801. decrfunc : string;
  802. begin
  803. if is_interfacecom(t) then
  804. decrfunc:='FPC_INTF_DECR_REF'
  805. else if is_ansistring(t) then
  806. decrfunc:='FPC_ANSISTR_DECR_REF'
  807. else if is_widestring(t) then
  808. decrfunc:='FPC_WIDESTR_DECR_REF'
  809. else if is_dynamic_array(t) then
  810. decrfunc:='FPC_DYNARRAY_INCR_REF'
  811. else
  812. decrfunc:='';
  813. { call the special decr function or the generic decref }
  814. if decrfunc<>'' then
  815. begin
  816. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  817. a_call_name(list,decrfunc);
  818. end
  819. else
  820. begin
  821. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  822. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  823. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  824. a_call_name(list,'FPC_DECREF');
  825. end;
  826. end;
  827. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  828. var
  829. href : treference;
  830. begin
  831. if is_ansistring(t) or
  832. is_widestring(t) or
  833. is_interfacecom(t) then
  834. a_load_const_ref(list,OS_ADDR,0,ref)
  835. else
  836. begin
  837. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  838. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  839. if loadref then
  840. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  841. else
  842. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  843. a_call_name(list,'FPC_INITIALIZE');
  844. end;
  845. end;
  846. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  847. var
  848. href : treference;
  849. begin
  850. if is_ansistring(t) or
  851. is_widestring(t) or
  852. is_interfacecom(t) then
  853. g_decrrefcount(list,t,ref)
  854. else
  855. begin
  856. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  857. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  858. if loadref then
  859. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  860. else
  861. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  862. a_call_name(list,'FPC_FINALIZE');
  863. end;
  864. end;
  865. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  866. { generate range checking code for the value at location p. The type }
  867. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  868. { is the original type used at that location. When both defs are equal }
  869. { the check is also insert (needed for succ,pref,inc,dec) }
  870. const
  871. {$ifdef ver1_0}
  872. awordsignedmax=high(longint);
  873. {$else}
  874. awordsignedmax=high(aword) div 2;
  875. {$endif}
  876. var
  877. neglabel : tasmlabel;
  878. hreg : tregister;
  879. fromdef : tdef;
  880. lto,hto,
  881. lfrom,hfrom : TConstExprInt;
  882. from_signed: boolean;
  883. begin
  884. { range checking on and range checkable value? }
  885. if not(cs_check_range in aktlocalswitches) or
  886. not(todef.deftype in [orddef,enumdef,arraydef]) then
  887. exit;
  888. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  889. begin
  890. cg64.g_rangecheck64(list,p,todef);
  891. exit;
  892. end;
  893. { only check when assigning to scalar, subranges are different, }
  894. { when todef=fromdef then the check is always generated }
  895. fromdef:=p.resulttype.def;
  896. getrange(p.resulttype.def,lfrom,hfrom);
  897. getrange(todef,lto,hto);
  898. { no range check if from and to are equal and are both longint/dword }
  899. { (if we have a 32bit processor) or int64/qword, since such }
  900. { operations can at most cause overflows (JM) }
  901. { Note that these checks are mostly processor independent, they only }
  902. { have to be changed once we introduce 64bit subrange types }
  903. if (fromdef = todef) and
  904. (fromdef.deftype=orddef) and
  905. (((sizeof(aword) = 4) and
  906. (((torddef(fromdef).typ = s32bit) and
  907. (lfrom = low(longint)) and
  908. (hfrom = high(longint))) or
  909. ((torddef(fromdef).typ = u32bit) and
  910. (lfrom = low(cardinal)) and
  911. (hfrom = high(cardinal)))))) then
  912. exit;
  913. if todef<>fromdef then
  914. begin
  915. { if the from-range falls completely in the to-range, no check }
  916. { is necessary }
  917. if (lto<=lfrom) and (hto>=hfrom) then
  918. exit;
  919. end;
  920. { generate the rangecheck code for the def where we are going to }
  921. { store the result }
  922. { use the trick that }
  923. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  924. { To be able to do that, we have to make sure however that either }
  925. { fromdef and todef are both signed or unsigned, or that we leave }
  926. { the parts < 0 and > maxlongint out }
  927. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  928. from_signed := is_signed(fromdef);
  929. if from_signed xor is_signed(todef) then
  930. if from_signed then
  931. { from is signed, to is unsigned }
  932. begin
  933. { if high(from) < 0 -> always range error }
  934. if (hfrom < 0) or
  935. { if low(to) > maxlongint also range error }
  936. (lto > awordsignedmax) then
  937. begin
  938. a_call_name(list,'FPC_RANGEERROR');
  939. exit
  940. end;
  941. { from is signed and to is unsigned -> when looking at from }
  942. { as an unsigned value, it must be < maxlongint (otherwise }
  943. { it's negative, which is invalid since "to" is unsigned) }
  944. if hto > awordsignedmax then
  945. hto := awordsignedmax;
  946. end
  947. else
  948. { from is unsigned, to is signed }
  949. begin
  950. if (lfrom > awordsignedmax) or
  951. (hto < 0) then
  952. begin
  953. a_call_name(list,'FPC_RANGEERROR');
  954. exit
  955. end;
  956. { from is unsigned and to is signed -> when looking at to }
  957. { as an unsigned value, it must be >= 0 (since negative }
  958. { values are the same as values > maxlongint) }
  959. if lto < 0 then
  960. lto := 0;
  961. end;
  962. hreg := get_scratch_reg_int(list);
  963. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  964. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  965. aword(lto),p.location.register,hreg)
  966. else
  967. begin
  968. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  969. p.location.reference,hreg);
  970. a_op_const_reg(list,OP_SUB,aword(lto),hreg);
  971. end;
  972. getlabel(neglabel);
  973. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(hto-lto),hreg,neglabel);
  974. { !!! should happen right after the compare (JM) }
  975. free_scratch_reg(list,hreg);
  976. a_call_name(list,'FPC_RANGEERROR');
  977. a_label(list,neglabel);
  978. end;
  979. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  980. begin
  981. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  982. a_call_name(list,'FPC_STACKCHECK');
  983. end;
  984. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  985. var
  986. tmpreg : tregister;
  987. begin
  988. tmpreg := get_scratch_reg_int(list);
  989. g_flags2reg(list,size,f,tmpreg);
  990. a_load_reg_ref(list,size,tmpreg,ref);
  991. free_scratch_reg(list,tmpreg);
  992. end;
  993. procedure tcg.g_maybe_loadself(list : taasmoutput);
  994. var
  995. hp : treference;
  996. p : pprocinfo;
  997. i : longint;
  998. begin
  999. if assigned(procinfo^._class) then
  1000. begin
  1001. list.concat(tai_regalloc.Alloc(SELF_POINTER_REG));
  1002. if lexlevel>normal_function_level then
  1003. begin
  1004. reference_reset_base(hp,procinfo^.framepointer,procinfo^.framepointer_offset);
  1005. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1006. p:=procinfo^.parent;
  1007. for i:=3 to lexlevel-1 do
  1008. begin
  1009. reference_reset_base(hp,SELF_POINTER_REG,p^.framepointer_offset);
  1010. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1011. p:=p^.parent;
  1012. end;
  1013. reference_reset_base(hp,SELF_POINTER_REG,p^.selfpointer_offset);
  1014. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1015. end
  1016. else
  1017. begin
  1018. reference_reset_base(hp,procinfo^.framepointer,procinfo^.selfpointer_offset);
  1019. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1020. end;
  1021. end;
  1022. end;
  1023. {*****************************************************************************
  1024. Entry/Exit Code Functions
  1025. *****************************************************************************}
  1026. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1027. var
  1028. href : treference;
  1029. hregister : tregister;
  1030. begin
  1031. if is_class(procinfo^._class) then
  1032. begin
  1033. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1034. { parameter 2 : self pointer / flag }
  1035. {!! this is a terrible hack, normally the helper should get three params : }
  1036. { one with self register, one with flag and one with VMT pointer }
  1037. {reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);}
  1038. a_param_reg(list, OS_ADDR, SELF_POINTER_REG, paramanager.getintparaloc(2));
  1039. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1040. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1041. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1042. a_call_name(list,'FPC_NEW_CLASS');
  1043. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1044. { save the self pointer result }
  1045. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1046. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1047. end
  1048. else if is_object(procinfo^._class) then
  1049. begin
  1050. { parameter 3 :vmt_offset }
  1051. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1052. { parameter 2 : address of pointer to vmt }
  1053. { this is the first(?) parameter which was pushed to the constructor }
  1054. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1055. hregister:=get_scratch_reg_address(list);
  1056. a_loadaddr_ref_reg(list, href, hregister);
  1057. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1058. free_scratch_reg(list, hregister);
  1059. { parameter 1 : address of self pointer }
  1060. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1061. hregister:=get_scratch_reg_address(list);
  1062. a_loadaddr_ref_reg(list, href, hregister);
  1063. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1064. free_scratch_reg(list, hregister);
  1065. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1066. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1067. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1068. end
  1069. else
  1070. internalerror(200006161);
  1071. end;
  1072. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1073. var
  1074. nofinal : tasmlabel;
  1075. href : treference;
  1076. hregister : tregister;
  1077. begin
  1078. if is_class(procinfo^._class) then
  1079. begin
  1080. { 2nd parameter : flag }
  1081. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);
  1082. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1083. { 1st parameter to destructor : self }
  1084. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1085. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1086. a_call_name(list,'FPC_DISPOSE_CLASS')
  1087. end
  1088. else if is_object(procinfo^._class) then
  1089. begin
  1090. { must the object be finalized ? }
  1091. if procinfo^._class.needs_inittable then
  1092. begin
  1093. getlabel(nofinal);
  1094. reference_reset_base(href,procinfo^.framepointer,target_info.first_parm_offset);
  1095. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1096. reference_reset_base(href,SELF_POINTER_REG,0);
  1097. g_finalize(list,procinfo^._class,href,false);
  1098. a_label(list,nofinal);
  1099. end;
  1100. { actually call destructor }
  1101. { parameter 3 :vmt_offset }
  1102. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1103. { parameter 2 : pointer to vmt }
  1104. { this is the first parameter which was pushed to the destructor }
  1105. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1106. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1107. { parameter 1 : address of self pointer }
  1108. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1109. hregister:=get_scratch_reg_address(list);
  1110. a_loadaddr_ref_reg(list, href, hregister);
  1111. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1112. free_scratch_reg(list, hregister);
  1113. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1114. end
  1115. else
  1116. internalerror(200006162);
  1117. end;
  1118. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1119. var
  1120. href : treference;
  1121. hregister : tregister;
  1122. begin
  1123. if is_class(procinfo^._class) then
  1124. begin
  1125. {
  1126. Dispose of the class then set self_pointer to nil
  1127. both in stack and in self register.
  1128. }
  1129. { 2nd parameter : flag }
  1130. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1131. { 1st parameter to destructor : self }
  1132. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1133. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1134. a_call_name(list,'FPC_DISPOSE_CLASS');
  1135. { SET SELF TO NIL }
  1136. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1137. { set the self pointer in the stack to nil }
  1138. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1139. end
  1140. else if is_object(procinfo^._class) then
  1141. begin
  1142. { parameter 3 :vmt_offset }
  1143. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1144. { parameter 2 : address of pointer to vmt }
  1145. { this is the first(?) parameter which was pushed to the constructor }
  1146. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1147. hregister:=get_scratch_reg_address(list);
  1148. a_loadaddr_ref_reg(list, href, hregister);
  1149. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1150. free_scratch_reg(list, hregister);
  1151. { parameter 1 : address of self pointer }
  1152. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1153. hregister:=get_scratch_reg_address(list);
  1154. a_loadaddr_ref_reg(list, href, hregister);
  1155. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1156. free_scratch_reg(list, hregister);
  1157. a_call_name(list,'FPC_HELP_FAIL');
  1158. { SET SELF TO NIL }
  1159. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1160. end
  1161. else
  1162. internalerror(200006163);
  1163. end;
  1164. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1165. begin
  1166. end;
  1167. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1168. begin
  1169. end;
  1170. procedure tcg.g_profilecode(list : taasmoutput);
  1171. begin
  1172. end;
  1173. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  1174. begin
  1175. a_load64_reg_reg(list,regsrc,regdst);
  1176. a_op64_const_reg(list,op,value,regdst);
  1177. end;
  1178. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1179. begin
  1180. a_load64_reg_reg(list,regsrc2,regdst);
  1181. a_op64_reg_reg(list,op,regsrc1,regdst);
  1182. end;
  1183. finalization
  1184. cg.free;
  1185. cg64.free;
  1186. end.
  1187. {
  1188. $Log$
  1189. Revision 1.39 2002-07-28 15:56:00 jonas
  1190. + tcg64.a_op64_const_reg_reg() and tcg64.a_op64_reg_reg_reg() methods +
  1191. generic implementation
  1192. Revision 1.38 2002/07/27 19:53:51 jonas
  1193. + generic implementation of tcg.g_flags2ref()
  1194. * tcg.flags2xxx() now also needs a size parameter
  1195. Revision 1.37 2002/07/20 11:57:53 florian
  1196. * types.pas renamed to defbase.pas because D6 contains a types
  1197. unit so this would conflicts if D6 programms are compiled
  1198. + Willamette/SSE2 instructions to assembler added
  1199. Revision 1.36 2002/07/11 14:41:27 florian
  1200. * start of the new generic parameter handling
  1201. Revision 1.35 2002/07/07 10:16:29 florian
  1202. * problems with last commit fixed
  1203. Revision 1.33 2002/07/07 09:52:32 florian
  1204. * powerpc target fixed, very simple units can be compiled
  1205. * some basic stuff for better callparanode handling, far from being finished
  1206. Revision 1.32 2002/07/06 20:09:10 carl
  1207. * updated generic constructor / destructor calling
  1208. Revision 1.31 2002/07/02 11:40:00 jonas
  1209. * fixed cg64 memory leak
  1210. Revision 1.30 2002/07/01 18:46:22 peter
  1211. * internal linker
  1212. * reorganized aasm layer
  1213. Revision 1.29 2002/07/01 16:23:52 peter
  1214. * cg64 patch
  1215. * basics for currency
  1216. * asnode updates for class and interface (not finished)
  1217. Revision 1.28 2002/06/06 18:53:17 jonas
  1218. * fixed internalerror(10) with -Or for i386 (a_load_ref_ref now saves
  1219. a general purpose register if it needs one but none are available)
  1220. Revision 1.27 2002/05/22 19:02:16 carl
  1221. + generic FPC_HELP_FAIL
  1222. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  1223. + generic FPC_DISPOSE_CLASS
  1224. + TEST_GENERIC define
  1225. Revision 1.26 2002/05/20 13:30:40 carl
  1226. * bugfix of hdisponen (base must be set, not index)
  1227. * more portability fixes
  1228. Revision 1.25 2002/05/18 13:34:05 peter
  1229. * readded missing revisions
  1230. Revision 1.24 2002/05/16 19:46:35 carl
  1231. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1232. + try to fix temp allocation (still in ifdef)
  1233. + generic constructor calls
  1234. + start of tassembler / tmodulebase class cleanup
  1235. Revision 1.23 2002/05/14 19:34:40 peter
  1236. * removed old logs and updated copyright year
  1237. Revision 1.22 2002/05/13 19:54:36 peter
  1238. * removed n386ld and n386util units
  1239. * maybe_save/maybe_restore added instead of the old maybe_push
  1240. Revision 1.21 2002/05/12 19:57:16 carl
  1241. * maybe_loadself portable
  1242. Revision 1.20 2002/05/12 16:53:04 peter
  1243. * moved entry and exitcode to ncgutil and cgobj
  1244. * foreach gets extra argument for passing local data to the
  1245. iterator function
  1246. * -CR checks also class typecasts at runtime by changing them
  1247. into as
  1248. * fixed compiler to cycle with the -CR option
  1249. * fixed stabs with elf writer, finally the global variables can
  1250. be watched
  1251. * removed a lot of routines from cga unit and replaced them by
  1252. calls to cgobj
  1253. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1254. u32bit then the other is typecasted also to u32bit without giving
  1255. a rangecheck warning/error.
  1256. * fixed pascal calling method with reversing also the high tree in
  1257. the parast, detected by tcalcst3 test
  1258. Revision 1.19 2002/04/26 15:19:04 peter
  1259. * use saveregisters for incr routines, saves also problems with
  1260. the optimizer
  1261. Revision 1.18 2002/04/25 20:16:38 peter
  1262. * moved more routines from cga/n386util
  1263. Revision 1.17 2002/04/22 16:30:05 peter
  1264. * fixed @methodpointer
  1265. Revision 1.16 2002/04/21 15:25:30 carl
  1266. + a_jmp_cond -> a_jmp_always (a_jmp_cond is NOT portable)
  1267. + changeregsize -> rg.makeregsize
  1268. Revision 1.15 2002/04/20 21:32:23 carl
  1269. + generic FPC_CHECKPOINTER
  1270. + first parameter offset in stack now portable
  1271. * rename some constants
  1272. + move some cpu stuff to other units
  1273. - remove unused constents
  1274. * fix stacksize for some targets
  1275. * fix generic size problems which depend now on EXTEND_SIZE constant
  1276. Revision 1.14 2002/04/15 19:44:18 peter
  1277. * fixed stackcheck that would be called recursively when a stack
  1278. error was found
  1279. * generic changeregsize(reg,size) for i386 register resizing
  1280. * removed some more routines from cga unit
  1281. * fixed returnvalue handling
  1282. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1283. Revision 1.13 2002/04/07 13:22:11 carl
  1284. + more documentation
  1285. Revision 1.12 2002/04/07 09:12:46 carl
  1286. + documentation
  1287. Revision 1.11 2002/04/06 18:10:42 jonas
  1288. * several powerpc-related additions and fixes
  1289. Revision 1.10 2002/04/04 19:05:54 peter
  1290. * removed unused units
  1291. * use tlocation.size in cg.a_*loc*() routines
  1292. Revision 1.9 2002/04/02 17:11:27 peter
  1293. * tlocation,treference update
  1294. * LOC_CONSTANT added for better constant handling
  1295. * secondadd splitted in multiple routines
  1296. * location_force_reg added for loading a location to a register
  1297. of a specified size
  1298. * secondassignment parses now first the right and then the left node
  1299. (this is compatible with Kylix). This saves a lot of push/pop especially
  1300. with string operations
  1301. * adapted some routines to use the new cg methods
  1302. Revision 1.8 2002/03/31 20:26:33 jonas
  1303. + a_loadfpu_* and a_loadmm_* methods in tcg
  1304. * register allocation is now handled by a class and is mostly processor
  1305. independent (+rgobj.pas and i386/rgcpu.pas)
  1306. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1307. * some small improvements and fixes to the optimizer
  1308. * some register allocation fixes
  1309. * some fpuvaroffset fixes in the unary minus node
  1310. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1311. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1312. also better optimizable)
  1313. * fixed and optimized register saving/restoring for new/dispose nodes
  1314. * LOC_FPU locations now also require their "register" field to be set to
  1315. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1316. - list field removed of the tnode class because it's not used currently
  1317. and can cause hard-to-find bugs
  1318. Revision 1.7 2002/03/04 19:10:11 peter
  1319. * removed compiler warnings
  1320. }