cgobj.pas 56 KB

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