cgobj.pas 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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; const f: tresflags; reg: TRegister); virtual; abstract;
  201. procedure g_flags2ref(list: taasmoutput; const f: tresflags; const ref:TReference); virtual; abstract;
  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_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : tparalocation);virtual;abstract;
  310. procedure a_param64_const(list : taasmoutput;value : qword;const loc : tparalocation);virtual;abstract;
  311. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : tparalocation);virtual;abstract;
  312. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : tparalocation);virtual;abstract;
  313. { override to catch 64bit rangechecks }
  314. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  315. const todef: tdef);virtual;abstract;
  316. end;
  317. var
  318. {# Main code generator class }
  319. cg : tcg;
  320. {# Code generator class for all operations working with 64-Bit operands }
  321. cg64 : tcg64;
  322. implementation
  323. uses
  324. globals,globtype,options,systems,cgbase,
  325. verbose,defbase,tgobj,symdef,paramgr,
  326. rgobj;
  327. const
  328. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  329. {*****************************************************************************
  330. basic functionallity
  331. ******************************************************************************}
  332. constructor tcg.create;
  333. var
  334. i : longint;
  335. begin
  336. scratch_register_array_pointer:=1;
  337. for i:=low(scratch_regs) to high(scratch_regs) do
  338. include(unusedscratchregisters,scratch_regs[i]);
  339. end;
  340. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  341. begin
  342. list.concat(tai_regalloc.alloc(r));
  343. end;
  344. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  345. begin
  346. list.concat(tai_regalloc.dealloc(r));
  347. end;
  348. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  349. begin
  350. list.concat(tai_label.create(l));
  351. end;
  352. function tcg.get_scratch_reg_int(list : taasmoutput) : tregister;
  353. var
  354. r : tregister;
  355. i : longint;
  356. begin
  357. if unusedscratchregisters=[] then
  358. internalerror(68996);
  359. for i:=scratch_register_array_pointer to
  360. (scratch_register_array_pointer+max_scratch_regs-1) do
  361. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  362. begin
  363. r:=scratch_regs[(i mod max_scratch_regs)+1];
  364. break;
  365. end;
  366. exclude(unusedscratchregisters,r);
  367. inc(scratch_register_array_pointer);
  368. if scratch_register_array_pointer>max_scratch_regs then
  369. scratch_register_array_pointer:=1;
  370. a_reg_alloc(list,r);
  371. get_scratch_reg_int:=r;
  372. end;
  373. { the default behavior simply returns a general purpose register }
  374. function tcg.get_scratch_reg_address(list : taasmoutput) : tregister;
  375. begin
  376. get_scratch_reg_address := get_scratch_reg_int(list);
  377. end;
  378. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  379. begin
  380. include(unusedscratchregisters,rg.makeregsize(r,OS_INT));
  381. a_reg_dealloc(list,r);
  382. end;
  383. {*****************************************************************************
  384. for better code generation these methods should be overridden
  385. ******************************************************************************}
  386. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  387. var
  388. ref : treference;
  389. begin
  390. case locpara.loc of
  391. LOC_REGISTER:
  392. a_load_reg_reg(list,size,r,locpara.register);
  393. LOC_REFERENCE:
  394. begin
  395. reference_reset(ref);
  396. ref.base:=locpara.reference.index;
  397. ref.offset:=locpara.reference.offset;
  398. a_load_reg_ref(list,size,r,ref);
  399. {!!!! FIX ME!, take sp_fixup into account }
  400. internalerror(2002071005);
  401. end
  402. else
  403. internalerror(2002071004);
  404. end;
  405. end;
  406. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  407. var
  408. hr : tregister;
  409. begin
  410. hr:=get_scratch_reg_int(list);
  411. a_load_const_reg(list,size,a,hr);
  412. a_param_reg(list,size,hr,locpara);
  413. free_scratch_reg(list,hr);
  414. end;
  415. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  416. var
  417. hr : tregister;
  418. begin
  419. hr:=get_scratch_reg_int(list);
  420. a_load_ref_reg(list,size,r,hr);
  421. a_param_reg(list,size,hr,locpara);
  422. free_scratch_reg(list,hr);
  423. end;
  424. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  425. begin
  426. case l.loc of
  427. LOC_REGISTER,
  428. LOC_CREGISTER :
  429. a_param_reg(list,l.size,l.register,locpara);
  430. LOC_CONSTANT :
  431. a_param_const(list,l.size,l.value,locpara);
  432. LOC_CREFERENCE,
  433. LOC_REFERENCE :
  434. a_param_ref(list,l.size,l.reference,locpara);
  435. else
  436. internalerror(2002032211);
  437. end;
  438. end;
  439. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  440. var
  441. hr : tregister;
  442. begin
  443. hr:=get_scratch_reg_address(list);
  444. a_loadaddr_ref_reg(list,r,hr);
  445. a_param_reg(list,OS_ADDR,hr,locpara);
  446. free_scratch_reg(list,hr);
  447. end;
  448. {****************************************************************************
  449. some generic implementations
  450. ****************************************************************************}
  451. procedure tcg.a_load_ref_ref(list : taasmoutput;size : tcgsize;const sref : treference;const dref : treference);
  452. var
  453. tmpreg: tregister;
  454. {$ifdef i386}
  455. pushed_reg: tregister;
  456. {$endif i386}
  457. begin
  458. {$ifdef i386}
  459. { the following is done with defines to avoid a speed penalty, }
  460. { since all this is only necessary for the 80x86 (because EDI }
  461. { doesn't have an 8bit component which is directly addressable) }
  462. pushed_reg := R_NO;
  463. if size in [OS_8,OS_S8] then
  464. if (rg.countunusedregsint = 0) then
  465. begin
  466. if (dref.base <> R_EBX) and
  467. (dref.index <> R_EBX) then
  468. pushed_reg := R_EBX
  469. else if (dref.base <> R_EAX) and
  470. (dref.index <> R_EAX) then
  471. pushed_reg := R_EAX
  472. else pushed_reg := R_ECX;
  473. tmpreg := rg.makeregsize(pushed_reg,OS_8);
  474. list.concat(taicpu.op_reg(A_PUSH,S_L,pushed_reg));
  475. end
  476. else
  477. tmpreg := rg.getregisterint(exprasmlist)
  478. else
  479. {$endif i386}
  480. tmpreg := get_scratch_reg_int(list);
  481. tmpreg:=rg.makeregsize(tmpreg,size);
  482. a_load_ref_reg(list,size,sref,tmpreg);
  483. a_load_reg_ref(list,size,tmpreg,dref);
  484. {$ifdef i386}
  485. if size in [OS_8,OS_S8] then
  486. begin
  487. if (pushed_reg <> R_NO) then
  488. list.concat(taicpu.op_reg(A_POP,S_L,pushed_reg))
  489. else
  490. rg.ungetregister(exprasmlist,tmpreg)
  491. end
  492. else
  493. {$endif i386}
  494. free_scratch_reg(list,tmpreg);
  495. end;
  496. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  497. var
  498. tmpreg: tregister;
  499. begin
  500. tmpreg := get_scratch_reg_int(list);
  501. a_load_const_reg(list,size,a,tmpreg);
  502. a_load_reg_ref(list,size,tmpreg,ref);
  503. free_scratch_reg(list,tmpreg);
  504. end;
  505. procedure tcg.a_load_const_loc(list : taasmoutput;a : aword;const loc: tlocation);
  506. begin
  507. case loc.loc of
  508. LOC_REFERENCE,LOC_CREFERENCE:
  509. a_load_const_ref(list,loc.size,a,loc.reference);
  510. LOC_REGISTER,LOC_CREGISTER:
  511. a_load_const_reg(list,loc.size,a,loc.register);
  512. else
  513. internalerror(200203272);
  514. end;
  515. end;
  516. procedure tcg.a_load_reg_loc(list : taasmoutput;size : tcgsize;reg : tregister;const loc: tlocation);
  517. begin
  518. case loc.loc of
  519. LOC_REFERENCE,LOC_CREFERENCE:
  520. a_load_reg_ref(list,size,reg,loc.reference);
  521. LOC_REGISTER,LOC_CREGISTER:
  522. a_load_reg_reg(list,size,reg,loc.register);
  523. else
  524. internalerror(200203271);
  525. end;
  526. end;
  527. procedure tcg.a_load_loc_reg(list : taasmoutput;const loc: tlocation; reg : tregister);
  528. begin
  529. case loc.loc of
  530. LOC_REFERENCE,LOC_CREFERENCE:
  531. a_load_ref_reg(list,loc.size,loc.reference,reg);
  532. LOC_REGISTER,LOC_CREGISTER:
  533. a_load_reg_reg(list,loc.size,loc.register,reg);
  534. LOC_CONSTANT:
  535. a_load_const_reg(list,loc.size,loc.value,reg);
  536. else
  537. internalerror(200109092);
  538. end;
  539. end;
  540. procedure tcg.a_load_loc_ref(list : taasmoutput;const loc: tlocation; const ref : treference);
  541. begin
  542. case loc.loc of
  543. LOC_REFERENCE,LOC_CREFERENCE:
  544. a_load_ref_ref(list,loc.size,loc.reference,ref);
  545. LOC_REGISTER,LOC_CREGISTER:
  546. a_load_reg_ref(list,loc.size,loc.register,ref);
  547. LOC_CONSTANT:
  548. a_load_const_ref(list,loc.size,loc.value,ref);
  549. else
  550. internalerror(200109302);
  551. end;
  552. end;
  553. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  554. begin
  555. case loc.loc of
  556. LOC_REFERENCE, LOC_CREFERENCE:
  557. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  558. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  559. a_loadfpu_reg_reg(list,loc.register,reg);
  560. else
  561. internalerror(200203301);
  562. end;
  563. end;
  564. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  565. begin
  566. case loc.loc of
  567. LOC_REFERENCE, LOC_CREFERENCE:
  568. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  569. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  570. a_loadfpu_reg_reg(list,reg,loc.register);
  571. else
  572. internalerror(48991);
  573. end;
  574. end;
  575. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  576. var
  577. tmpreg: tregister;
  578. begin
  579. tmpreg := get_scratch_reg_int(list);
  580. a_load_ref_reg(list,size,ref,tmpreg);
  581. a_op_const_reg(list,op,a,tmpreg);
  582. a_load_reg_ref(list,size,tmpreg,ref);
  583. free_scratch_reg(list,tmpreg);
  584. end;
  585. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: AWord; const loc: tlocation);
  586. begin
  587. case loc.loc of
  588. LOC_REGISTER, LOC_CREGISTER:
  589. a_op_const_reg(list,op,a,loc.register);
  590. LOC_REFERENCE, LOC_CREFERENCE:
  591. a_op_const_ref(list,op,loc.size,a,loc.reference);
  592. else
  593. internalerror(200109061);
  594. end;
  595. end;
  596. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  597. var
  598. tmpreg: tregister;
  599. begin
  600. tmpreg := get_scratch_reg_int(list);
  601. a_load_ref_reg(list,size,ref,tmpreg);
  602. a_op_reg_reg(list,op,size,reg,tmpreg);
  603. a_load_reg_ref(list,size,tmpreg,ref);
  604. free_scratch_reg(list,tmpreg);
  605. end;
  606. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  607. var
  608. tmpreg: tregister;
  609. begin
  610. case op of
  611. OP_NOT,OP_NEG:
  612. { handle it as "load ref,reg; op reg" }
  613. begin
  614. a_load_ref_reg(list,size,ref,reg);
  615. a_op_reg_reg(list,op,size,reg,reg);
  616. end;
  617. else
  618. begin
  619. tmpreg := get_scratch_reg_int(list);
  620. a_load_ref_reg(list,size,ref,tmpreg);
  621. a_op_reg_reg(list,op,size,tmpreg,reg);
  622. free_scratch_reg(list,tmpreg);
  623. end;
  624. end;
  625. end;
  626. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  627. begin
  628. case loc.loc of
  629. LOC_REGISTER, LOC_CREGISTER:
  630. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  631. LOC_REFERENCE, LOC_CREFERENCE:
  632. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  633. else
  634. internalerror(200109061);
  635. end;
  636. end;
  637. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  638. var
  639. tmpreg: tregister;
  640. begin
  641. case loc.loc of
  642. LOC_REGISTER,LOC_CREGISTER:
  643. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  644. LOC_REFERENCE,LOC_CREFERENCE:
  645. begin
  646. tmpreg := get_scratch_reg_int(list);
  647. tmpreg:=rg.makeregsize(tmpreg,loc.size);
  648. a_load_ref_reg(list,loc.size,ref,tmpreg);
  649. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  650. free_scratch_reg(list,tmpreg);
  651. end;
  652. else
  653. internalerror(200109061);
  654. end;
  655. end;
  656. procedure tcg.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  657. size: tcgsize; a: aword; src, dst: tregister);
  658. begin
  659. a_load_reg_reg(list,size,src,dst);
  660. a_op_const_reg(list,op,a,dst);
  661. end;
  662. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  663. size: tcgsize; src1, src2, dst: tregister);
  664. begin
  665. a_load_reg_reg(list,size,src2,dst);
  666. a_op_reg_reg(list,op,size,src1,dst);
  667. end;
  668. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  669. l : tasmlabel);
  670. var
  671. tmpreg: tregister;
  672. begin
  673. tmpreg := get_scratch_reg_int(list);
  674. a_load_ref_reg(list,size,ref,tmpreg);
  675. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  676. free_scratch_reg(list,tmpreg);
  677. end;
  678. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  679. l : tasmlabel);
  680. begin
  681. case loc.loc of
  682. LOC_REGISTER,LOC_CREGISTER:
  683. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  684. LOC_REFERENCE,LOC_CREFERENCE:
  685. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  686. else
  687. internalerror(200109061);
  688. end;
  689. end;
  690. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  691. var
  692. tmpreg: tregister;
  693. begin
  694. tmpreg := get_scratch_reg_int(list);
  695. a_load_ref_reg(list,size,ref,tmpreg);
  696. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  697. free_scratch_reg(list,tmpreg);
  698. end;
  699. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  700. begin
  701. case loc.loc of
  702. LOC_REGISTER,
  703. LOC_CREGISTER:
  704. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  705. LOC_REFERENCE,
  706. LOC_CREFERENCE :
  707. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  708. LOC_CONSTANT:
  709. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  710. else
  711. internalerror(200203231);
  712. end;
  713. end;
  714. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  715. l : tasmlabel);
  716. var
  717. tmpreg: tregister;
  718. begin
  719. case loc.loc of
  720. LOC_REGISTER,LOC_CREGISTER:
  721. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  722. LOC_REFERENCE,LOC_CREFERENCE:
  723. begin
  724. {$ifdef i386}
  725. { the following is done with defines to avoid a speed penalty, }
  726. { since all this is only necessary for the 80x86 (because EDI }
  727. { doesn't have an 8bit component which is directly addressable) }
  728. if size in [OS_8,OS_S8] then
  729. tmpreg := rg.getregisterint(exprasmlist)
  730. else
  731. {$endif i386}
  732. tmpreg := get_scratch_reg_int(list);
  733. tmpreg := rg.makeregsize(tmpreg,size);
  734. a_load_ref_reg(list,size,loc.reference,tmpreg);
  735. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  736. {$ifdef i386}
  737. if size in [OS_8,OS_S8] then
  738. rg.ungetregister(exprasmlist,tmpreg)
  739. else
  740. {$endif i386}
  741. free_scratch_reg(list,tmpreg);
  742. end
  743. else
  744. internalerror(200109061);
  745. end;
  746. end;
  747. function tcg.reg_cgsize(const reg: tregister) : tcgsize;
  748. begin
  749. reg_cgsize := OS_INT;
  750. end;
  751. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte;delsource,loadref : boolean);
  752. begin
  753. {$warning FIX ME!}
  754. a_paramaddr_ref(list,dest,paramanager.getintparaloc(3));
  755. if loadref then
  756. a_param_ref(list,OS_ADDR,source,paramanager.getintparaloc(2))
  757. else
  758. a_paramaddr_ref(list,source,paramanager.getintparaloc(2));
  759. if delsource then
  760. reference_release(list,source);
  761. a_param_const(list,OS_INT,len,paramanager.getintparaloc(1));
  762. a_call_name(list,'FPC_SHORTSTR_COPY');
  763. g_maybe_loadself(list);
  764. end;
  765. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  766. var
  767. href : treference;
  768. incrfunc : string;
  769. begin
  770. { These functions should not change the registers (they use
  771. the saveregister proc directive }
  772. if is_interfacecom(t) then
  773. incrfunc:='FPC_INTF_INCR_REF'
  774. else if is_ansistring(t) then
  775. incrfunc:='FPC_ANSISTR_INCR_REF'
  776. else if is_widestring(t) then
  777. incrfunc:='FPC_WIDESTR_INCR_REF'
  778. else if is_dynamic_array(t) then
  779. incrfunc:='FPC_DYNARRAY_INCR_REF'
  780. else
  781. incrfunc:='';
  782. { call the special incr function or the generic addref }
  783. if incrfunc<>'' then
  784. begin
  785. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1));
  786. a_call_name(list,incrfunc);
  787. end
  788. else
  789. begin
  790. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  791. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  792. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  793. a_call_name(list,'FPC_ADDREF');
  794. end;
  795. end;
  796. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  797. var
  798. href : treference;
  799. decrfunc : string;
  800. begin
  801. if is_interfacecom(t) then
  802. decrfunc:='FPC_INTF_DECR_REF'
  803. else if is_ansistring(t) then
  804. decrfunc:='FPC_ANSISTR_DECR_REF'
  805. else if is_widestring(t) then
  806. decrfunc:='FPC_WIDESTR_DECR_REF'
  807. else if is_dynamic_array(t) then
  808. decrfunc:='FPC_DYNARRAY_INCR_REF'
  809. else
  810. decrfunc:='';
  811. { call the special decr function or the generic decref }
  812. if decrfunc<>'' then
  813. begin
  814. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  815. a_call_name(list,decrfunc);
  816. end
  817. else
  818. begin
  819. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  820. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  821. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  822. a_call_name(list,'FPC_DECREF');
  823. end;
  824. end;
  825. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  826. var
  827. href : treference;
  828. begin
  829. if is_ansistring(t) or
  830. is_widestring(t) or
  831. is_interfacecom(t) then
  832. a_load_const_ref(list,OS_ADDR,0,ref)
  833. else
  834. begin
  835. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  836. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  837. if loadref then
  838. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  839. else
  840. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  841. a_call_name(list,'FPC_INITIALIZE');
  842. end;
  843. end;
  844. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;loadref : boolean);
  845. var
  846. href : treference;
  847. begin
  848. if is_ansistring(t) or
  849. is_widestring(t) or
  850. is_interfacecom(t) then
  851. g_decrrefcount(list,t,ref)
  852. else
  853. begin
  854. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  855. a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  856. if loadref then
  857. a_param_ref(list,OS_ADDR,ref,paramanager.getintparaloc(1))
  858. else
  859. a_paramaddr_ref(list,ref,paramanager.getintparaloc(1));
  860. a_call_name(list,'FPC_FINALIZE');
  861. end;
  862. end;
  863. procedure tcg.g_rangecheck(list: taasmoutput; const p: tnode;const todef: tdef);
  864. { generate range checking code for the value at location p. The type }
  865. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  866. { is the original type used at that location. When both defs are equal }
  867. { the check is also insert (needed for succ,pref,inc,dec) }
  868. const
  869. {$ifdef ver1_0}
  870. awordsignedmax=high(longint);
  871. {$else}
  872. awordsignedmax=high(aword) div 2;
  873. {$endif}
  874. var
  875. neglabel : tasmlabel;
  876. hreg : tregister;
  877. fromdef : tdef;
  878. lto,hto,
  879. lfrom,hfrom : TConstExprInt;
  880. from_signed: boolean;
  881. begin
  882. { range checking on and range checkable value? }
  883. if not(cs_check_range in aktlocalswitches) or
  884. not(todef.deftype in [orddef,enumdef,arraydef]) then
  885. exit;
  886. if is_64bitint(p.resulttype.def) or is_64bitint(todef) then
  887. begin
  888. cg64.g_rangecheck64(list,p,todef);
  889. exit;
  890. end;
  891. { only check when assigning to scalar, subranges are different, }
  892. { when todef=fromdef then the check is always generated }
  893. fromdef:=p.resulttype.def;
  894. getrange(p.resulttype.def,lfrom,hfrom);
  895. getrange(todef,lto,hto);
  896. { no range check if from and to are equal and are both longint/dword }
  897. { (if we have a 32bit processor) or int64/qword, since such }
  898. { operations can at most cause overflows (JM) }
  899. { Note that these checks are mostly processor independent, they only }
  900. { have to be changed once we introduce 64bit subrange types }
  901. if (fromdef = todef) and
  902. (fromdef.deftype=orddef) and
  903. (((sizeof(aword) = 4) and
  904. (((torddef(fromdef).typ = s32bit) and
  905. (lfrom = low(longint)) and
  906. (hfrom = high(longint))) or
  907. ((torddef(fromdef).typ = u32bit) and
  908. (lfrom = low(cardinal)) and
  909. (hfrom = high(cardinal))))) or
  910. is_64bitint(fromdef)) then
  911. exit;
  912. if todef<>fromdef then
  913. begin
  914. { if the from-range falls completely in the to-range, no check }
  915. { is necessary }
  916. if (lto<=lfrom) and (hto>=hfrom) then
  917. exit;
  918. end;
  919. { generate the rangecheck code for the def where we are going to }
  920. { store the result }
  921. { use the trick that }
  922. { a <= x <= b <=> 0 <= x-a <= b-a <=> cardinal(x-a) <= cardinal(b-a) }
  923. { To be able to do that, we have to make sure however that either }
  924. { fromdef and todef are both signed or unsigned, or that we leave }
  925. { the parts < 0 and > maxlongint out }
  926. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  927. from_signed := is_signed(fromdef);
  928. if from_signed xor is_signed(todef) then
  929. if from_signed then
  930. { from is signed, to is unsigned }
  931. begin
  932. { if high(from) < 0 -> always range error }
  933. if (hfrom < 0) or
  934. { if low(to) > maxlongint also range error }
  935. (lto > awordsignedmax) then
  936. begin
  937. a_call_name(list,'FPC_RANGEERROR');
  938. exit
  939. end;
  940. { from is signed and to is unsigned -> when looking at from }
  941. { as an unsigned value, it must be < maxlongint (otherwise }
  942. { it's negative, which is invalid since "to" is unsigned) }
  943. if hto > awordsignedmax then
  944. hto := awordsignedmax;
  945. end
  946. else
  947. { from is unsigned, to is signed }
  948. begin
  949. if (lfrom > awordsignedmax) or
  950. (hto < 0) then
  951. begin
  952. a_call_name(list,'FPC_RANGEERROR');
  953. exit
  954. end;
  955. { from is unsigned and to is signed -> when looking at to }
  956. { as an unsigned value, it must be >= 0 (since negative }
  957. { values are the same as values > maxlongint) }
  958. if lto < 0 then
  959. lto := 0;
  960. end;
  961. hreg := get_scratch_reg_int(list);
  962. if (p.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  963. a_op_const_reg_reg(list,OP_SUB,def_cgsize(p.resulttype.def),
  964. aword(longint(lto and $ffffffff)),p.location.register,hreg)
  965. else
  966. begin
  967. a_load_ref_reg(list,def_cgsize(p.resulttype.def),
  968. p.location.reference,hreg);
  969. a_op_const_reg(list,OP_SUB,aword(longint(lto and $ffffffff)),hreg);
  970. end;
  971. getlabel(neglabel);
  972. a_cmp_const_reg_label(list,OS_INT,OC_BE,aword(longint((hto-lto) and $ffffffff)),hreg,neglabel);
  973. { !!! should happen right after the compare (JM) }
  974. free_scratch_reg(list,hreg);
  975. a_call_name(list,'FPC_RANGEERROR');
  976. a_label(list,neglabel);
  977. end;
  978. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  979. begin
  980. a_param_const(list,OS_32,stackframesize,paramanager.getintparaloc(1));
  981. a_call_name(list,'FPC_STACKCHECK');
  982. end;
  983. procedure tcg.g_maybe_loadself(list : taasmoutput);
  984. var
  985. hp : treference;
  986. p : pprocinfo;
  987. i : longint;
  988. begin
  989. if assigned(procinfo^._class) then
  990. begin
  991. list.concat(tai_regalloc.Alloc(SELF_POINTER_REG));
  992. if lexlevel>normal_function_level then
  993. begin
  994. reference_reset_base(hp,procinfo^.framepointer,procinfo^.framepointer_offset);
  995. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  996. p:=procinfo^.parent;
  997. for i:=3 to lexlevel-1 do
  998. begin
  999. reference_reset_base(hp,SELF_POINTER_REG,p^.framepointer_offset);
  1000. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1001. p:=p^.parent;
  1002. end;
  1003. reference_reset_base(hp,SELF_POINTER_REG,p^.selfpointer_offset);
  1004. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1005. end
  1006. else
  1007. begin
  1008. reference_reset_base(hp,procinfo^.framepointer,procinfo^.selfpointer_offset);
  1009. a_load_ref_reg(list,OS_ADDR,hp,SELF_POINTER_REG);
  1010. end;
  1011. end;
  1012. end;
  1013. {*****************************************************************************
  1014. Entry/Exit Code Functions
  1015. *****************************************************************************}
  1016. procedure tcg.g_call_constructor_helper(list : taasmoutput);
  1017. var
  1018. href : treference;
  1019. hregister : tregister;
  1020. begin
  1021. if is_class(procinfo^._class) then
  1022. begin
  1023. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1024. { parameter 2 : self pointer / flag }
  1025. {!! this is a terrible hack, normally the helper should get three params : }
  1026. { one with self register, one with flag and one with VMT pointer }
  1027. {reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);}
  1028. a_param_reg(list, OS_ADDR, SELF_POINTER_REG, paramanager.getintparaloc(2));
  1029. { parameter 1 : vmt pointer (stored at the selfpointer address on stack) }
  1030. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1031. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1032. a_call_name(list,'FPC_NEW_CLASS');
  1033. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1034. { save the self pointer result }
  1035. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1036. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1037. end
  1038. else if is_object(procinfo^._class) then
  1039. begin
  1040. { parameter 3 :vmt_offset }
  1041. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1042. { parameter 2 : address of pointer to vmt }
  1043. { this is the first(?) parameter which was pushed to the constructor }
  1044. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1045. hregister:=get_scratch_reg_address(list);
  1046. a_loadaddr_ref_reg(list, href, hregister);
  1047. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1048. free_scratch_reg(list, hregister);
  1049. { parameter 1 : address of self pointer }
  1050. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1051. hregister:=get_scratch_reg_address(list);
  1052. a_loadaddr_ref_reg(list, href, hregister);
  1053. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1054. free_scratch_reg(list, hregister);
  1055. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1056. a_load_reg_reg(list,OS_ADDR,accumulator,SELF_POINTER_REG);
  1057. a_cmp_const_reg_label(list,OS_ADDR,OC_EQ,0,accumulator,faillabel);
  1058. end
  1059. else
  1060. internalerror(200006161);
  1061. end;
  1062. procedure tcg.g_call_destructor_helper(list : taasmoutput);
  1063. var
  1064. nofinal : tasmlabel;
  1065. href : treference;
  1066. hregister : tregister;
  1067. begin
  1068. if is_class(procinfo^._class) then
  1069. begin
  1070. { 2nd parameter : flag }
  1071. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset+POINTER_SIZE);
  1072. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(2));
  1073. { 1st parameter to destructor : self }
  1074. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1075. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1076. a_call_name(list,'FPC_DISPOSE_CLASS')
  1077. end
  1078. else if is_object(procinfo^._class) then
  1079. begin
  1080. { must the object be finalized ? }
  1081. if procinfo^._class.needs_inittable then
  1082. begin
  1083. getlabel(nofinal);
  1084. reference_reset_base(href,procinfo^.framepointer,target_info.first_parm_offset);
  1085. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1086. reference_reset_base(href,SELF_POINTER_REG,0);
  1087. g_finalize(list,procinfo^._class,href,false);
  1088. a_label(list,nofinal);
  1089. end;
  1090. { actually call destructor }
  1091. { parameter 3 :vmt_offset }
  1092. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1093. { parameter 2 : pointer to vmt }
  1094. { this is the first parameter which was pushed to the destructor }
  1095. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1096. a_param_ref(list, OS_ADDR, href ,paramanager.getintparaloc(2));
  1097. { parameter 1 : address of self pointer }
  1098. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1099. hregister:=get_scratch_reg_address(list);
  1100. a_loadaddr_ref_reg(list, href, hregister);
  1101. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1102. free_scratch_reg(list, hregister);
  1103. a_call_name(list,'FPC_HELP_DESTRUCTOR');
  1104. end
  1105. else
  1106. internalerror(200006162);
  1107. end;
  1108. procedure tcg.g_call_fail_helper(list : taasmoutput);
  1109. var
  1110. href : treference;
  1111. hregister : tregister;
  1112. begin
  1113. if is_class(procinfo^._class) then
  1114. begin
  1115. {
  1116. Dispose of the class then set self_pointer to nil
  1117. both in stack and in self register.
  1118. }
  1119. { 2nd parameter : flag }
  1120. a_param_const(list,OS_32,1,paramanager.getintparaloc(2));
  1121. { 1st parameter to destructor : self }
  1122. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1123. a_param_ref(list, OS_ADDR,href,paramanager.getintparaloc(1));
  1124. a_call_name(list,'FPC_DISPOSE_CLASS');
  1125. { SET SELF TO NIL }
  1126. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1127. { set the self pointer in the stack to nil }
  1128. a_load_reg_ref(list,OS_ADDR,SELF_POINTER_REG,href);
  1129. end
  1130. else if is_object(procinfo^._class) then
  1131. begin
  1132. { parameter 3 :vmt_offset }
  1133. a_param_const(list, OS_32, procinfo^._class.vmt_offset, paramanager.getintparaloc(3));
  1134. { parameter 2 : address of pointer to vmt }
  1135. { this is the first(?) parameter which was pushed to the constructor }
  1136. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset-POINTER_SIZE);
  1137. hregister:=get_scratch_reg_address(list);
  1138. a_loadaddr_ref_reg(list, href, hregister);
  1139. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(2));
  1140. free_scratch_reg(list, hregister);
  1141. { parameter 1 : address of self pointer }
  1142. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1143. hregister:=get_scratch_reg_address(list);
  1144. a_loadaddr_ref_reg(list, href, hregister);
  1145. a_param_reg(list, OS_ADDR,hregister,paramanager.getintparaloc(1));
  1146. free_scratch_reg(list, hregister);
  1147. a_call_name(list,'FPC_HELP_FAIL');
  1148. { SET SELF TO NIL }
  1149. a_load_const_reg(list,OS_ADDR,0,SELF_POINTER_REG);
  1150. end
  1151. else
  1152. internalerror(200006163);
  1153. end;
  1154. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  1155. begin
  1156. end;
  1157. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1158. begin
  1159. end;
  1160. procedure tcg.g_profilecode(list : taasmoutput);
  1161. begin
  1162. end;
  1163. finalization
  1164. cg.free;
  1165. cg64.free;
  1166. end.
  1167. {
  1168. $Log$
  1169. Revision 1.37 2002-07-20 11:57:53 florian
  1170. * types.pas renamed to defbase.pas because D6 contains a types
  1171. unit so this would conflicts if D6 programms are compiled
  1172. + Willamette/SSE2 instructions to assembler added
  1173. Revision 1.36 2002/07/11 14:41:27 florian
  1174. * start of the new generic parameter handling
  1175. Revision 1.35 2002/07/07 10:16:29 florian
  1176. * problems with last commit fixed
  1177. Revision 1.33 2002/07/07 09:52:32 florian
  1178. * powerpc target fixed, very simple units can be compiled
  1179. * some basic stuff for better callparanode handling, far from being finished
  1180. Revision 1.32 2002/07/06 20:09:10 carl
  1181. * updated generic constructor / destructor calling
  1182. Revision 1.31 2002/07/02 11:40:00 jonas
  1183. * fixed cg64 memory leak
  1184. Revision 1.30 2002/07/01 18:46:22 peter
  1185. * internal linker
  1186. * reorganized aasm layer
  1187. Revision 1.29 2002/07/01 16:23:52 peter
  1188. * cg64 patch
  1189. * basics for currency
  1190. * asnode updates for class and interface (not finished)
  1191. Revision 1.28 2002/06/06 18:53:17 jonas
  1192. * fixed internalerror(10) with -Or for i386 (a_load_ref_ref now saves
  1193. a general purpose register if it needs one but none are available)
  1194. Revision 1.27 2002/05/22 19:02:16 carl
  1195. + generic FPC_HELP_FAIL
  1196. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  1197. + generic FPC_DISPOSE_CLASS
  1198. + TEST_GENERIC define
  1199. Revision 1.26 2002/05/20 13:30:40 carl
  1200. * bugfix of hdisponen (base must be set, not index)
  1201. * more portability fixes
  1202. Revision 1.25 2002/05/18 13:34:05 peter
  1203. * readded missing revisions
  1204. Revision 1.24 2002/05/16 19:46:35 carl
  1205. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1206. + try to fix temp allocation (still in ifdef)
  1207. + generic constructor calls
  1208. + start of tassembler / tmodulebase class cleanup
  1209. Revision 1.23 2002/05/14 19:34:40 peter
  1210. * removed old logs and updated copyright year
  1211. Revision 1.22 2002/05/13 19:54:36 peter
  1212. * removed n386ld and n386util units
  1213. * maybe_save/maybe_restore added instead of the old maybe_push
  1214. Revision 1.21 2002/05/12 19:57:16 carl
  1215. * maybe_loadself portable
  1216. Revision 1.20 2002/05/12 16:53:04 peter
  1217. * moved entry and exitcode to ncgutil and cgobj
  1218. * foreach gets extra argument for passing local data to the
  1219. iterator function
  1220. * -CR checks also class typecasts at runtime by changing them
  1221. into as
  1222. * fixed compiler to cycle with the -CR option
  1223. * fixed stabs with elf writer, finally the global variables can
  1224. be watched
  1225. * removed a lot of routines from cga unit and replaced them by
  1226. calls to cgobj
  1227. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1228. u32bit then the other is typecasted also to u32bit without giving
  1229. a rangecheck warning/error.
  1230. * fixed pascal calling method with reversing also the high tree in
  1231. the parast, detected by tcalcst3 test
  1232. Revision 1.19 2002/04/26 15:19:04 peter
  1233. * use saveregisters for incr routines, saves also problems with
  1234. the optimizer
  1235. Revision 1.18 2002/04/25 20:16:38 peter
  1236. * moved more routines from cga/n386util
  1237. Revision 1.17 2002/04/22 16:30:05 peter
  1238. * fixed @methodpointer
  1239. Revision 1.16 2002/04/21 15:25:30 carl
  1240. + a_jmp_cond -> a_jmp_always (a_jmp_cond is NOT portable)
  1241. + changeregsize -> rg.makeregsize
  1242. Revision 1.15 2002/04/20 21:32:23 carl
  1243. + generic FPC_CHECKPOINTER
  1244. + first parameter offset in stack now portable
  1245. * rename some constants
  1246. + move some cpu stuff to other units
  1247. - remove unused constents
  1248. * fix stacksize for some targets
  1249. * fix generic size problems which depend now on EXTEND_SIZE constant
  1250. Revision 1.14 2002/04/15 19:44:18 peter
  1251. * fixed stackcheck that would be called recursively when a stack
  1252. error was found
  1253. * generic changeregsize(reg,size) for i386 register resizing
  1254. * removed some more routines from cga unit
  1255. * fixed returnvalue handling
  1256. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1257. Revision 1.13 2002/04/07 13:22:11 carl
  1258. + more documentation
  1259. Revision 1.12 2002/04/07 09:12:46 carl
  1260. + documentation
  1261. Revision 1.11 2002/04/06 18:10:42 jonas
  1262. * several powerpc-related additions and fixes
  1263. Revision 1.10 2002/04/04 19:05:54 peter
  1264. * removed unused units
  1265. * use tlocation.size in cg.a_*loc*() routines
  1266. Revision 1.9 2002/04/02 17:11:27 peter
  1267. * tlocation,treference update
  1268. * LOC_CONSTANT added for better constant handling
  1269. * secondadd splitted in multiple routines
  1270. * location_force_reg added for loading a location to a register
  1271. of a specified size
  1272. * secondassignment parses now first the right and then the left node
  1273. (this is compatible with Kylix). This saves a lot of push/pop especially
  1274. with string operations
  1275. * adapted some routines to use the new cg methods
  1276. Revision 1.8 2002/03/31 20:26:33 jonas
  1277. + a_loadfpu_* and a_loadmm_* methods in tcg
  1278. * register allocation is now handled by a class and is mostly processor
  1279. independent (+rgobj.pas and i386/rgcpu.pas)
  1280. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1281. * some small improvements and fixes to the optimizer
  1282. * some register allocation fixes
  1283. * some fpuvaroffset fixes in the unary minus node
  1284. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1285. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1286. also better optimizable)
  1287. * fixed and optimized register saving/restoring for new/dispose nodes
  1288. * LOC_FPU locations now also require their "register" field to be set to
  1289. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1290. - list field removed of the tnode class because it's not used currently
  1291. and can cause hard-to-find bugs
  1292. Revision 1.7 2002/03/04 19:10:11 peter
  1293. * removed compiler warnings
  1294. }