cgobj.pas 55 KB

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