cgobj.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. unit cgobj;
  20. {$i defines.inc}
  21. interface
  22. uses
  23. cclasses,aasm,symtable,cpuasm,cpubase,cgbase,cpuinfo,
  24. symconst,symbase,symtype;
  25. type
  26. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  27. tcg = class
  28. scratch_register_array_pointer : aword;
  29. unusedscratchregisters : tregisterset;
  30. alignment : talignment;
  31. {************************************************}
  32. { basic routines }
  33. constructor create;
  34. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  35. { allocates register r by inserting a pai_realloc record }
  36. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  37. { deallocates register r by inserting a pa_regdealloc record}
  38. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  39. { returns a register for use as scratch register }
  40. function get_scratch_reg(list : taasmoutput) : tregister;
  41. { releases a scratch register }
  42. procedure free_scratch_reg(list : taasmoutput;r : tregister);
  43. {************************************************}
  44. { code generation for subroutine entry/exit code }
  45. { initilizes data of type t }
  46. { if is_already_ref is true then the routines assumes }
  47. { that r points to the data to initialize }
  48. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  49. { finalizes data of type t }
  50. { if is_already_ref is true then the routines assumes }
  51. { that r points to the data to finalizes }
  52. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  53. { helper routines }
  54. procedure g_initialize_data(list : taasmoutput;p : tsym);
  55. procedure g_incr_data(list : taasmoutput;p : tsym);
  56. procedure g_finalize_data(list : taasmoutput;p : tnamedindexitem);
  57. procedure g_copyvalueparas(list : taasmoutput;p : tnamedindexitem);
  58. procedure g_finalizetempansistrings(list : taasmoutput);
  59. procedure g_entrycode(alist : TAAsmoutput;make_global:boolean;
  60. stackframe:longint;
  61. var parasize:longint;var nostackframe:boolean;
  62. inlined : boolean);
  63. procedure g_exitcode(list : taasmoutput;parasize : longint;
  64. nostackframe,inlined : boolean);
  65. { string helper routines }
  66. procedure g_decrstrref(list : taasmoutput;const ref : treference;t : tdef);
  67. procedure g_removetemps(list : taasmoutput;p : tlinkedlist);
  68. { passing parameters, per default the parameter is pushed }
  69. { nr gives the number of the parameter (enumerated from }
  70. { left to right), this allows to move the parameter to }
  71. { register, if the cpu supports register calling }
  72. { conventions }
  73. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);virtual; abstract;
  74. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);virtual;
  75. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);virtual;
  76. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);virtual;
  77. {**********************************}
  78. { these methods must be overriden: }
  79. { Remarks:
  80. * If a method specifies a size you have only to take care
  81. of that number of bits, i.e. load_const_reg with OP_8 must
  82. only load the lower 8 bit of the specified register
  83. the rest of the register can be undefined
  84. if necessary the compiler will call a method
  85. to zero or sign extend the register
  86. * The a_load_XX_XX with OP_64 needn't to be
  87. implemented for 32 bit
  88. processors, the code generator takes care of that
  89. * the addr size is for work with the natural pointer
  90. size
  91. * the procedures without fpu/mm are only for integer usage
  92. * normally the first location is the source and the
  93. second the destination
  94. }
  95. procedure a_call_name(list : taasmoutput;const s : string;
  96. offset : longint);virtual; abstract;
  97. { move instructions }
  98. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aword;register : tregister);virtual; abstract;
  99. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  100. procedure a_load_reg_ref(list : taasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  101. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  102. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  103. procedure a_load_loc_reg(list : taasmoutput;size : tcgsize;const loc: tlocation; reg : tregister);
  104. { basic arithmetic operations }
  105. { note: for operators which require only one argument (not, neg), use }
  106. { the op_reg_reg, op_reg_reg or op_reg_loc methods and keep in mind }
  107. { that in this case the *second* operand is used as both source and }
  108. { destination (JM) }
  109. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); virtual; abstract;
  110. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); virtual;
  111. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const loc: tlocation);
  112. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  113. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  114. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  115. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: tregister; const loc: tlocation);
  116. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; const loc: tlocation);
  117. { comparison operations }
  118. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  119. l : tasmlabel);virtual; abstract;
  120. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  121. l : tasmlabel); virtual;
  122. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aword; const loc: tlocation;
  123. l : tasmlabel); virtual;
  124. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  125. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  126. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  127. l : tasmlabel);
  128. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel); virtual; abstract;
  129. procedure g_flags2reg(list: taasmoutput; const f: tresflags; reg: TRegister); virtual; abstract;
  130. procedure a_loadaddress_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  131. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual; abstract;
  132. { restores the frame pointer at procedure exit, for the }
  133. { i386 it generates a simple leave }
  134. procedure g_restore_frame_pointer(list : taasmoutput);virtual; abstract;
  135. { some processors like the PPC doesn't allow to change the stack in }
  136. { a procedure, so we need to maintain an extra stack for the }
  137. { result values of setjmp in exception code }
  138. { this two procedures are for pushing an exception value, }
  139. { they can use the scratch registers }
  140. procedure g_push_exception_value_reg(list : taasmoutput;reg : tregister);virtual; abstract;
  141. procedure g_push_exception_value_const(list : taasmoutput;reg : tregister);virtual; abstract;
  142. { that procedure pops a exception value }
  143. procedure g_pop_exception_value_reg(list : taasmoutput;reg : tregister);virtual; abstract;
  144. procedure g_return_from_proc(list : taasmoutput;parasize : aword);virtual; abstract;
  145. {********************************************************}
  146. { these methods can be overriden for extra functionality }
  147. { the following methods do nothing: }
  148. procedure g_interrupt_stackframe_entry(list : taasmoutput);virtual;
  149. procedure g_interrupt_stackframe_exit(list : taasmoutput);virtual;
  150. procedure g_profilecode(list : taasmoutput);virtual;
  151. procedure g_stackcheck(list : taasmoutput;stackframesize : longint);virtual;
  152. procedure g_maybe_loadself(list : taasmoutput);virtual; abstract;
  153. { copies len bytes from the source to destination, if }
  154. { loadref is true, it assumes that it first must load }
  155. { the source address from the memory location where }
  156. { source points to }
  157. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword;delsource,loadref : boolean);virtual; abstract;
  158. {$ifdef i386}
  159. { this one is only necessary due the the restrictions of the 80x86, }
  160. { so make it a special case (JM) }
  161. function makeregsize(var reg: tregister; size: tcgsize): topsize; virtual; abstract;
  162. {$endif i386}
  163. end;
  164. var
  165. cg : tcg; { this is the main code generator class }
  166. implementation
  167. uses
  168. strings,globals,globtype,options,{files,}gdb,systems,
  169. ppu,verbose,types,{tgobj,}tgcpu,symdef,symsym,cga
  170. {$IFDEF NEWST}
  171. ,symbols,defs,symtablt
  172. {$ENDIF NEWST};
  173. const
  174. max_scratch_regs = high(scratch_regs) - low(scratch_regs) + 1;
  175. {*****************************************************************************
  176. basic functionallity
  177. ******************************************************************************}
  178. constructor tcg.create;
  179. var
  180. i : longint;
  181. begin
  182. scratch_register_array_pointer:=1;
  183. for i:=low(scratch_regs) to high(scratch_regs) do
  184. include(unusedscratchregisters,scratch_regs[i]);
  185. end;
  186. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  187. begin
  188. list.concat(tairegalloc.alloc(r));
  189. end;
  190. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  191. begin
  192. list.concat(tairegalloc.dealloc(r));
  193. end;
  194. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  195. begin
  196. list.concat(tai_label.create(l));
  197. end;
  198. function tcg.get_scratch_reg(list : taasmoutput) : tregister;
  199. var
  200. r : tregister;
  201. i : longint;
  202. begin
  203. if unusedscratchregisters=[] then
  204. internalerror(68996);
  205. for i:=scratch_register_array_pointer to
  206. (scratch_register_array_pointer+max_scratch_regs-1) do
  207. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  208. begin
  209. r:=scratch_regs[(i mod max_scratch_regs)+1];
  210. break;
  211. end;
  212. exclude(unusedscratchregisters,r);
  213. inc(scratch_register_array_pointer);
  214. if scratch_register_array_pointer>max_scratch_regs then
  215. scratch_register_array_pointer:=1;
  216. a_reg_alloc(list,r);
  217. get_scratch_reg:=r;
  218. end;
  219. procedure tcg.free_scratch_reg(list : taasmoutput;r : tregister);
  220. begin
  221. include(unusedscratchregisters,makereg32(r));
  222. a_reg_dealloc(list,r);
  223. end;
  224. {*****************************************************************************
  225. this methods must be overridden for extra functionality
  226. ******************************************************************************}
  227. procedure tcg.g_interrupt_stackframe_entry(list : taasmoutput);
  228. begin
  229. end;
  230. procedure tcg.g_interrupt_stackframe_exit(list : taasmoutput);
  231. begin
  232. end;
  233. procedure tcg.g_profilecode(list : taasmoutput);
  234. begin
  235. end;
  236. {*****************************************************************************
  237. for better code generation these methods should be overridden
  238. ******************************************************************************}
  239. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);
  240. var
  241. hr : tregister;
  242. begin
  243. hr:=get_scratch_reg(list);
  244. a_load_const_reg(list,size,a,hr);
  245. a_param_reg(list,size,hr,nr);
  246. free_scratch_reg(list,hr);
  247. end;
  248. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);
  249. var
  250. hr : tregister;
  251. begin
  252. hr:=get_scratch_reg(list);
  253. a_load_ref_reg(list,size,r,hr);
  254. a_param_reg(list,size,hr,nr);
  255. free_scratch_reg(list,hr);
  256. end;
  257. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);
  258. var
  259. hr : tregister;
  260. begin
  261. hr:=get_scratch_reg(list);
  262. a_loadaddress_ref_reg(list,r,hr);
  263. a_param_reg(list,OS_ADDR,hr,nr);
  264. free_scratch_reg(list,hr);
  265. end;
  266. procedure tcg.g_stackcheck(list : taasmoutput;stackframesize : longint);
  267. begin
  268. a_param_const(list,OS_32,stackframesize,1);
  269. a_call_name(list,'FPC_STACKCHECK',0);
  270. end;
  271. {*****************************************************************************
  272. String helper routines
  273. *****************************************************************************}
  274. procedure tcg.g_removetemps(list : taasmoutput;p : tlinkedlist);
  275. (*
  276. var
  277. hp : ptemptodestroy;
  278. pushedregs : tpushed;
  279. *)
  280. begin
  281. (*
  282. hp:=ptemptodestroy(p^.first);
  283. if not(assigned(hp)) then
  284. exit;
  285. tg.pushusedregisters(pushedregs,$ff);
  286. while assigned(hp) do
  287. begin
  288. if is_ansistring(hp^.typ) then
  289. begin
  290. g_decrstrref(list,hp^.address,hp^.typ);
  291. tg.ungetiftemp(hp^.address);
  292. end;
  293. hp:=ptemptodestroy(hp^.next);
  294. end;
  295. tg.popusedregisters(pushedregs);
  296. *)
  297. runerror(211);
  298. end;
  299. procedure tcg.g_decrstrref(list : taasmoutput;const ref : treference;t : tdef);
  300. var
  301. pushedregs : tpushed;
  302. begin
  303. (*
  304. tg.pushusedregisters(pushedregs,$ff);
  305. a_param_ref_addr(list,ref,1);
  306. if is_ansistring(t) then
  307. a_call_name(list,'FPC_ANSISTR_DECR_REF',0)
  308. else if is_widestring(t) then
  309. a_call_name(list,'FPC_WIDESTR_DECR_REF',0)
  310. else internalerror(58993);
  311. tg.popusedregisters(pushedregs);
  312. *)
  313. runerror(211);
  314. end;
  315. {*****************************************************************************
  316. Code generation for subroutine entry- and exit code
  317. *****************************************************************************}
  318. { initilizes data of type t }
  319. { if is_already_ref is true then the routines assumes }
  320. { that r points to the data to initialize }
  321. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  322. var
  323. hr : treference;
  324. begin
  325. (*
  326. if is_ansistring(t) or
  327. is_widestring(t) then
  328. a_load_const_ref(list,OS_8,0,ref)
  329. else
  330. begin
  331. reset_reference(hr);
  332. hr.symbol:=t^.get_inittable_label;
  333. a_param_ref_addr(list,hr,2);
  334. if is_already_ref then
  335. a_param_ref(list,OS_ADDR,ref,1)
  336. else
  337. a_param_ref_addr(list,ref,1);
  338. a_call_name(list,'FPC_INITIALIZE',0);
  339. end;
  340. *)
  341. runerror(211);
  342. end;
  343. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference;is_already_ref : boolean);
  344. var
  345. r : treference;
  346. begin
  347. (*
  348. if is_ansistring(t) or
  349. is_widestring(t) then
  350. begin
  351. g_decrstrref(list,ref,t);
  352. end
  353. else
  354. begin
  355. reset_reference(r);
  356. r.symbol:=t^.get_inittable_label;
  357. a_param_ref_addr(list,r,2);
  358. if is_already_ref then
  359. a_paramaddr_ref(list,ref,1)
  360. else
  361. a_param_ref_addr(list,ref,1);
  362. a_call_name(list,'FPC_FINALIZE',0);
  363. end;
  364. *)
  365. runerror(211);
  366. end;
  367. { generates the code for initialisation of local data }
  368. procedure tcg.g_initialize_data(list : taasmoutput;p : tsym);
  369. var
  370. hr : treference;
  371. begin
  372. (*
  373. {$IFDEF NEWST}
  374. if (typeof(p^)=typeof(Tvarsym)) and
  375. assigned(pvarsym(p)^.definition) and
  376. not((typeof((pvarsym(p)^.definition^))=typeof(Tobjectdef)) and
  377. (oo_is_class in pobjectdef(pvarsym(p)^.definition)^.options)) and
  378. pvarsym(p)^.definition^.needs_inittable then
  379. begin
  380. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  381. reset_reference(hr);
  382. if typeof((tsym(p)^.owner^))=typeof(Tprocsymtable) then
  383. begin
  384. hr.base:=procinfo^.framepointer;
  385. hr.offset:=-pvarsym(p)^.address;
  386. end
  387. else
  388. begin
  389. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  390. end;
  391. g_initialize(list,pvarsym(p)^.definition,hr,false);
  392. end;
  393. {$ELSE}
  394. if (tsym(p)^.typ=varsym) and
  395. assigned(pvarsym(p)^.vartype.def) and
  396. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  397. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  398. pvarsym(p)^.vartype.def^.needs_inittable then
  399. begin
  400. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  401. reset_reference(hr);
  402. if tsym(p)^.owner^.symtabletype=localsymtable then
  403. begin
  404. hr.base:=procinfo^.framepointer;
  405. hr.offset:=-pvarsym(p)^.address;
  406. end
  407. else
  408. begin
  409. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  410. end;
  411. g_initialize(list,pvarsym(p)^.vartype.def,hr,false);
  412. end;
  413. {$ENDIF NEWST}
  414. *)
  415. runerror(211);
  416. end;
  417. { generates the code for incrementing the reference count of parameters }
  418. procedure tcg.g_incr_data(list : taasmoutput;p : tsym);
  419. var
  420. hr : treference;
  421. begin
  422. (*
  423. {$IFDEF NEWST}
  424. if (typeof((tsym(p)^))=typeof(Tparamsym)) and
  425. not((typeof((Pparamsym(p)^.definition^))=typeof(Tobjectdef)) and
  426. (oo_is_class in pobjectdef(pvarsym(p)^.definition)^.options)) and
  427. Pparamsym(p)^.definition^.needs_inittable and
  428. ((Pparamsym(p)^.varspez=vs_value)) then
  429. begin
  430. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  431. reset_reference(hr);
  432. hr.symbol:=pvarsym(p)^.definition^.get_inittable_label;
  433. a_param_ref_addr(list,hr,2);
  434. reset_reference(hr);
  435. hr.base:=procinfo^.framepointer;
  436. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  437. a_param_ref_addr(list,hr,1);
  438. reset_reference(hr);
  439. a_call_name(list,'FPC_ADDREF',0);
  440. end;
  441. {$ELSE}
  442. if (tsym(p)^.typ=varsym) and
  443. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  444. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  445. pvarsym(p)^.vartype.def^.needs_inittable and
  446. ((pvarsym(p)^.varspez=vs_value)) then
  447. begin
  448. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  449. reset_reference(hr);
  450. hr.symbol:=pvarsym(p)^.vartype.def^.get_inittable_label;
  451. a_param_ref_addr(list,hr,2);
  452. reset_reference(hr);
  453. hr.base:=procinfo^.framepointer;
  454. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  455. a_param_ref_addr(list,hr,1);
  456. reset_reference(hr);
  457. a_call_name(list,'FPC_ADDREF',0);
  458. end;
  459. {$ENDIF NEWST}
  460. *)
  461. runerror(211);
  462. end;
  463. { generates the code for finalisation of local data }
  464. procedure tcg.g_finalize_data(list : taasmoutput;p : tnamedindexitem);
  465. var
  466. hr : treference;
  467. begin
  468. (*
  469. {$IFDEF NEWST}
  470. if (typeof((tsym(p)^))=typeof(Tvarsym)) and
  471. assigned(pvarsym(p)^.definition) and
  472. not((typeof((pvarsym(p)^.definition^))=typeof(Tobjectdef)) and
  473. (oo_is_class in pobjectdef(pvarsym(p)^.definition)^.options)) and
  474. pvarsym(p)^.definition^.needs_inittable then
  475. begin
  476. { not all kind of parameters need to be finalized }
  477. if (typeof((tsym(p)^.owner^))=typeof(Tprocsymtable)) and
  478. ((pparamsym(p)^.varspez=vs_var) or
  479. (Pparamsym(p)^.varspez=vs_const) { and
  480. (dont_copy_const_param(pvarsym(p)^.definition)) } ) then
  481. exit;
  482. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  483. reset_reference(hr);
  484. if typeof((tsym(p)^.owner^))=typeof(Tprocsymtable) then
  485. begin
  486. hr.base:=procinfo^.framepointer;
  487. hr.offset:=-pvarsym(p)^.address;
  488. end
  489. else if typeof((tsym(p)^.owner^))=typeof(Tprocsymtable) then
  490. begin
  491. hr.base:=procinfo^.framepointer;
  492. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  493. end
  494. else
  495. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  496. g_finalize(list,pvarsym(p)^.definition,hr,false);
  497. end;
  498. {$ELSE}
  499. if (tsym(p)^.typ=varsym) and
  500. assigned(pvarsym(p)^.vartype.def) and
  501. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  502. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  503. pvarsym(p)^.vartype.def^.needs_inittable then
  504. begin
  505. { not all kind of parameters need to be finalized }
  506. if (tsym(p)^.owner^.symtabletype=parasymtable) and
  507. ((pvarsym(p)^.varspez=vs_var) or
  508. (pvarsym(p)^.varspez=vs_const) { and
  509. (dont_copy_const_param(pvarsym(p)^.definition)) } ) then
  510. exit;
  511. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  512. reset_reference(hr);
  513. case tsym(p)^.owner^.symtabletype of
  514. localsymtable:
  515. begin
  516. hr.base:=procinfo^.framepointer;
  517. hr.offset:=-pvarsym(p)^.address;
  518. end;
  519. parasymtable:
  520. begin
  521. hr.base:=procinfo^.framepointer;
  522. hr.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  523. end;
  524. else
  525. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  526. end;
  527. g_finalize(list,pvarsym(p)^.vartype.def,hr,false);
  528. end;
  529. {$ENDIF NEWST}
  530. *)
  531. runerror(211);
  532. end;
  533. { generates the code to make local copies of the value parameters }
  534. procedure tcg.g_copyvalueparas(list : taasmoutput;p : tnamedindexitem);
  535. begin
  536. runerror(255);
  537. end;
  538. (*
  539. var
  540. _list : taasmoutput;
  541. { wrappers for the methods, because TP doesn't know procedures }
  542. { of objects }
  543. {$IFNDEF NEWST}
  544. procedure _copyvalueparas(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  545. begin
  546. cg^.g_copyvalueparas(_list,s);
  547. end;
  548. {$ENDIF NEWST}
  549. *)
  550. procedure tcg.g_finalizetempansistrings(list : taasmoutput);
  551. (*
  552. var
  553. hp : ptemprecord;
  554. hr : treference;
  555. *)
  556. begin
  557. (*
  558. hp:=tg.templist;
  559. while assigned(hp) do
  560. begin
  561. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  562. begin
  563. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  564. reset_reference(hr);
  565. hr.base:=procinfo^.framepointer;
  566. hr.offset:=hp^.pos;
  567. a_param_ref_addr(list,hr,1);
  568. a_call_name(list,'FPC_ANSISTR_DECR_REF',0);
  569. end;
  570. hp:=hp^.next;
  571. end;
  572. *)
  573. runerror(211);
  574. end;
  575. (*
  576. {$IFDEF NEWST}
  577. procedure _initialize_local(s:tnamedindexitem);{$IFNDEF FPC}far;{$ENDIF}
  578. begin
  579. if typeof(s^)=typeof(Tparamsym) then
  580. cg^.g_incr_data(_list,tsym(s))
  581. else
  582. cg^.g_initialize_data(_list,tsym(s));
  583. end;
  584. procedure _finalize_data(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  585. begin
  586. if typeof(s^)=typeof(Tvarsym) then
  587. cg^.g_finalize_data(_list,s);
  588. end;
  589. {$ELSE}
  590. procedure _finalize_data(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  591. begin
  592. cg^.g_finalize_data(_list,s);
  593. end;
  594. procedure _incr_data(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  595. begin
  596. cg^.g_incr_data(_list,tsym(s));
  597. end;
  598. procedure _initialize_data(s : tnamedindexitem);{$ifndef FPC}far;{$endif}
  599. begin
  600. cg^.g_initialize_data(_list,tsym(s));
  601. end;
  602. {$ENDIF NEWST}
  603. *)
  604. { generates the entry code for a procedure }
  605. procedure tcg.g_entrycode(alist : TAAsmoutput;make_global:boolean;
  606. stackframe:longint;
  607. var parasize:longint;var nostackframe:boolean;
  608. inlined : boolean);
  609. {$IFDEF NEWST}
  610. procedure _copyvalueparas(s:Pparamsym);{$ifndef FPC}far;{$endif}
  611. begin
  612. cg^.g_copyvalueparas(_list,s);
  613. end;
  614. {$ENDIF NEWST}
  615. (*
  616. var
  617. hs : string;
  618. hp : pused_unit;
  619. initcode : taasmoutput;
  620. {$ifdef GDB}
  621. stab_function_name : Pai_stab_function_name;
  622. {$endif GDB}
  623. hr : treference;
  624. r : tregister;
  625. *)
  626. begin
  627. (*
  628. { Align }
  629. if (not inlined) then
  630. begin
  631. { gprof uses 16 byte granularity !! }
  632. if (cs_profile in aktmoduleswitches) then
  633. list^.insert(new(pai_align,init(16)))
  634. else
  635. if not(cs_littlesize in aktglobalswitches) then
  636. list^.insert(new(pai_align,init(4)));
  637. end;
  638. { save registers on cdecl }
  639. {$IFDEF NEWST}
  640. if (posavestdregs in aktprocdef^.options) then
  641. {$ELSE}
  642. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  643. {$ENDIF NEWST}
  644. begin
  645. for r:=firstreg to lastreg do
  646. begin
  647. if (r in registers_saved_on_cdecl) then
  648. if (r in (tg.availabletempregsint+
  649. tg.availabletempregsfpu+
  650. tg.availabletempregsmm)) then
  651. begin
  652. if not(r in tg.usedinproc) then
  653. {!!!!!!!!!!!! a_push_reg(list,r) }
  654. end
  655. else
  656. {!!!!!!!! a_push_reg(list,r) };
  657. end;
  658. end;
  659. { omit stack frame ? }
  660. if not inlined then
  661. if procinfo^.framepointer=stack_pointer then
  662. begin
  663. CGMessage(cg_d_stackframe_omited);
  664. nostackframe:=true;
  665. {$IFDEF NEWST}
  666. if (aktprocdef^.proctype in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  667. parasize:=0
  668. else
  669. parasize:=aktprocdef^.localst^.paramdatasize+procinfo^.para_offset-pointersize;
  670. {$ELSE}
  671. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  672. parasize:=0
  673. else
  674. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-pointersize;
  675. {$ENDIF NEWST}
  676. end
  677. else
  678. begin
  679. {$IFDEF NEWST}
  680. if (aktprocdef^.proctype in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  681. parasize:=0
  682. else
  683. parasize:=aktprocdef^.localst^.paramdatasize+procinfo^.para_offset-pointersize*2;
  684. {$ELSE}
  685. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  686. parasize:=0
  687. else
  688. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-pointersize*2;
  689. {$ENDIF}
  690. nostackframe:=false;
  691. {$IFDEF NEWST}
  692. if (pointerrupt in aktprocdef^.options) then
  693. g_interrupt_stackframe_entry(list);
  694. {$ELSE}
  695. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  696. g_interrupt_stackframe_entry(list);
  697. {$ENDIF NEWST}
  698. g_stackframe_entry(list,stackframe);
  699. if (cs_check_stack in aktlocalswitches) and
  700. (tf_supports_stack_checking in target_info.flags) then
  701. g_stackcheck(@initcode,stackframe);
  702. end;
  703. if cs_profile in aktmoduleswitches then
  704. g_profilecode(@initcode);
  705. {$IFDEF NEWST}
  706. if (not inlined) and (aktprocdef^.proctype in [potype_unitinit]) then
  707. {$ELSE}
  708. if (not inlined) and (aktprocsym^.definition^.proctypeoption in [potype_unitinit]) then
  709. {$ENDIF NEWST}
  710. begin
  711. { needs the target a console flags ? }
  712. if tf_needs_isconsole in target_info.flags then
  713. begin
  714. hr.symbol:=newasmsymbol('U_'+target_info.system_unit+'_ISCONSOLE');
  715. if apptype=at_cui then
  716. a_load_const_ref(list,OS_8,1,hr)
  717. else
  718. a_load_const_ref(list,OS_8,0,hr);
  719. dispose(hr.symbol,done);
  720. end;
  721. hp:=pused_unit(usedunits.first);
  722. while assigned(hp) do
  723. begin
  724. { call the unit init code and make it external }
  725. if (hp^.u^.flags and uf_init)<>0 then
  726. a_call_name(list,
  727. 'INIT$$'+hp^.u^.modulename^,0);
  728. hp:=Pused_unit(hp^.next);
  729. end;
  730. end;
  731. {$ifdef dummy}
  732. { a constructor needs a help procedure }
  733. if (aktprocsym^.definition^.options and poconstructor)<>0 then
  734. begin
  735. if procinfo^._class^.isclass then
  736. begin
  737. list^.concat(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol('FPC_NEW_CLASS'))));
  738. list^.concat(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  739. end
  740. else
  741. begin
  742. {
  743. list^.insert(new(pai_labeled,init(A_JZ,quickexitlabel)));
  744. list^.insert(new(paicpu,op_csymbol(A_CALL,S_NO,
  745. newcsymbol('FPC_HELP_CONSTRUCTOR',0))));
  746. list^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
  747. concat_external('FPC_HELP_CONSTRUCTOR',EXT_NEAR);
  748. }
  749. end;
  750. end;
  751. {$endif dummy}
  752. {$ifdef GDB}
  753. if (cs_debuginfo in aktmoduleswitches) then
  754. list^.insert(new(pai_force_line,init));
  755. {$endif GDB}
  756. {$IFDEF NEWST}
  757. { initialize return value }
  758. if assigned(procinfo^.retdef) and
  759. is_ansistring(procinfo^.retdef) or
  760. is_widestring(procinfo^.retdef) then
  761. begin
  762. reset_reference(hr);
  763. hr.offset:=procinfo^.return_offset;
  764. hr.base:=procinfo^.framepointer;
  765. a_load_const_ref(list,OS_32,0,hr);
  766. end;
  767. {$ELSE}
  768. { initialize return value }
  769. if assigned(procinfo^.returntype.def) and
  770. is_ansistring(procinfo^.returntype.def) or
  771. is_widestring(procinfo^.returntype.def) then
  772. begin
  773. reset_reference(hr);
  774. hr.offset:=procinfo^.return_offset;
  775. hr.base:=procinfo^.framepointer;
  776. a_load_const_ref(list,OS_32,0,hr);
  777. end;
  778. {$ENDIF}
  779. _list:=list;
  780. { generate copies of call by value parameters }
  781. {$IFDEF NEWST}
  782. if (poassembler in aktprocdef^.options) then
  783. aktprocdef^.parameters^.foreach(@_copyvalueparas);
  784. {$ELSE}
  785. if (po_assembler in aktprocsym^.definition^.procoptions) then
  786. aktprocsym^.definition^.parast^.foreach({$ifdef FPC}@{$endif FPC}_copyvalueparas);
  787. {$ENDIF NEWST}
  788. {$IFDEF NEWST}
  789. { initialisizes local data }
  790. aktprocdef^.localst^.foreach({$ifdef FPC}@{$endif FPC}_initialize_local);
  791. {$ELSE}
  792. { initialisizes local data }
  793. aktprocsym^.definition^.localst^.foreach({$ifdef FPC}@{$endif FPC}_initialize_data);
  794. { add a reference to all call by value/const parameters }
  795. aktprocsym^.definition^.parast^.foreach({$ifdef FPC}@{$endif FPC}_incr_data);
  796. {$ENDIF NEWST}
  797. {$IFDEF NEWST}
  798. if (cs_profile in aktmoduleswitches) or
  799. (typeof(aktprocdef^.owner^)=typeof(Tglobalsymtable)) or
  800. (typeof(aktprocdef^.owner^)=typeof(Timplsymtable)) or
  801. (assigned(procinfo^._class) and
  802. (typeof(procinfo^._class^.owner^)=typeof(Tglobalsymtable)) or
  803. (typeof(procinfo^._class^.owner^)=typeof(Timplsymtable))) then
  804. make_global:=true;
  805. {$ELSE}
  806. if (cs_profile in aktmoduleswitches) or
  807. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  808. (assigned(procinfo^._class) and (procinfo^._class^.owner^.symtabletype=globalsymtable)) then
  809. make_global:=true;
  810. {$ENDIF NEWST}
  811. if not inlined then
  812. begin
  813. hs:=proc_names.get;
  814. {$ifdef GDB}
  815. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  816. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  817. {$endif GDB}
  818. { insert the names for the procedure }
  819. while hs<>'' do
  820. begin
  821. if make_global then
  822. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  823. else
  824. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  825. {$ifdef GDB}
  826. if (cs_debuginfo in aktmoduleswitches) then
  827. begin
  828. if target_os.use_function_relative_addresses then
  829. list^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  830. end;
  831. {$endif GDB}
  832. hs:=proc_names.get;
  833. end;
  834. end;
  835. {$ifdef GDB}
  836. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  837. begin
  838. if target_os.use_function_relative_addresses then
  839. list^.insert(stab_function_name);
  840. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  841. aktprocsym^.is_global := True;
  842. list^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  843. aktprocsym^.isstabwritten:=true;
  844. end;
  845. {$endif GDB}
  846. *)
  847. runerror(211);
  848. end;
  849. procedure tcg.g_exitcode(list : taasmoutput;parasize:longint;nostackframe,inlined:boolean);
  850. var
  851. {$ifdef GDB}
  852. mangled_length : longint;
  853. p : pchar;
  854. {$endif GDB}
  855. nofinal,noreraiselabel : tasmlabel;
  856. hr : treference;
  857. r : tregister;
  858. begin
  859. (*
  860. if aktexitlabel^.is_used then
  861. list^.insert(new(pai_label,init(aktexitlabel)));
  862. { call the destructor help procedure }
  863. {$IFDEF NEWST}
  864. if (aktprocdef^.proctype=potype_destructor) then
  865. {$ELSE}
  866. if (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  867. {$ENDIF}
  868. begin
  869. {$IFDEF NEWST}
  870. if oo_is_class in procinfo^._class^.options then
  871. {$ELSE NEWST}
  872. if procinfo^._class^.is_class then
  873. {$ENDIF}
  874. a_call_name(list,'FPC_DISPOSE_CLASS',0)
  875. else
  876. begin
  877. if procinfo^._class^.needs_inittable then
  878. begin
  879. getlabel(nofinal);
  880. {!!!!!!!!!!
  881. reset_reference(hr);
  882. hr.base:=R_EBP;
  883. hr.offset:=8;
  884. a_cmp_reg_const_label(list,OS_ADDR,OZ_EQ,
  885. }
  886. reset_reference(hr);
  887. hr.symbol:=procinfo^._class^.get_inittable_label;
  888. a_paramaddr_ref(list,hr,2);
  889. a_param_reg(list,OS_ADDR,self_pointer,1);
  890. a_call_name(list,'FPC_FINALIZE',0);
  891. a_label(list,nofinal);
  892. end;
  893. { vmt_offset_reg can be a scratch register, }
  894. { but it must be always the same }
  895. a_reg_alloc(list,vmt_offset_reg);
  896. a_load_const_reg(list,OS_32,procinfo^._class^.vmt_offset,vmt_offset_reg);
  897. a_call_name(list,'FPC_HELP_DESTRUCTOR',0);
  898. a_reg_dealloc(list,vmt_offset_reg);
  899. end;
  900. end;
  901. { finalize temporary data }
  902. g_finalizetempansistrings(list);
  903. _list:=list;
  904. { finalize local data }
  905. {$IFDEF NEWST}
  906. aktprocdef^.localst^.foreach({$ifndef TP}@{$endif}_finalize_data);
  907. {$ELSE}
  908. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}_finalize_data);
  909. {$ENDIF}
  910. {$IFNDEF NEWST}
  911. { finalize paras data }
  912. if assigned(aktprocsym^.definition^.parast) then
  913. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}_finalize_data);
  914. {$ENDIF NEWST}
  915. { do we need to handle exceptions because of ansi/widestrings ? }
  916. if (procinfo^.flags and pi_needs_implicit_finally)<>0 then
  917. begin
  918. getlabel(noreraiselabel);
  919. a_call_name(list,'FPC_POPADDRSTACK',0);
  920. a_reg_alloc(list,accumulator);
  921. g_pop_exception_value_reg(list,accumulator);
  922. a_cmp_const_reg_label(list,OS_32,OC_EQ,0,accumulator,noreraiselabel);
  923. a_reg_dealloc(list,accumulator);
  924. {$IFDEF NEWST}
  925. { must be the return value finalized before reraising the exception? }
  926. if (procinfo^.retdef<>tdef(voiddef)) and
  927. (procinfo^.retdef^.needs_inittable) and
  928. ((typeof(procinfo^.retdef^)<>typeof(Tobjectdef)) or
  929. not(oo_is_class in pobjectdef(procinfo^.retdef)^.options)) then
  930. begin
  931. reset_reference(hr);
  932. hr.offset:=procinfo^.return_offset;
  933. hr.base:=procinfo^.framepointer;
  934. g_finalize(list,procinfo^.retdef,hr,not (dp_ret_in_acc in procinfo^.retdef^.properties));
  935. end;
  936. {$ELSE}
  937. { must be the return value finalized before reraising the exception? }
  938. if (procinfo^.returntype.def<>tdef(voiddef)) and
  939. (procinfo^.returntype.def^.needs_inittable) and
  940. ((procinfo^.returntype.def^.deftype<>objectdef) or
  941. not(pobjectdef(procinfo^.returntype.def)^.is_class)) then
  942. begin
  943. reset_reference(hr);
  944. hr.offset:=procinfo^.return_offset;
  945. hr.base:=procinfo^.framepointer;
  946. g_finalize(list,procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  947. end;
  948. {$ENDIF}
  949. a_call_name(list,'FPC_RERAISE',0);
  950. a_label(list,noreraiselabel);
  951. end;
  952. { call __EXIT for main program }
  953. {$IFDEF NEWST}
  954. if (not DLLsource) and (not inlined) and (aktprocdef^.proctype=potype_proginit) then
  955. a_call_name(list,'FPC_DO_EXIT',0);
  956. {$ELSE}
  957. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  958. a_call_name(list,'FPC_DO_EXIT',0);
  959. {$ENDIF NEWST}
  960. { handle return value }
  961. {$IFDEF NEWST}
  962. if not(poassembler in aktprocdef^.options) then
  963. if (aktprocdef^.proctype<>potype_constructor) then
  964. {$ELSE}
  965. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  966. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  967. {$ENDIF NEWST}
  968. { handle_return_value(inlined) }
  969. else
  970. begin
  971. { return self in EAX }
  972. a_label(list,quickexitlabel);
  973. a_reg_alloc(list,accumulator);
  974. a_load_reg_reg(list,OS_ADDR,self_pointer,accumulator);
  975. a_reg_dealloc(list,self_pointer);
  976. a_label(list,quickexitlabel);
  977. { we can't clear the zero flag because the Alpha }
  978. { for example doesn't have flags, we have to compare }
  979. { the accu. in the caller }
  980. end;
  981. { stabs uses the label also ! }
  982. if aktexit2label^.is_used or
  983. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  984. a_label(list,aktexit2label);
  985. {$ifdef dummy}
  986. { should we restore edi ? }
  987. { for all i386 gcc implementations }
  988. {!!!!!!!!!!! I don't know how to handle register saving yet }
  989. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  990. begin
  991. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  992. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  993. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  994. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  995. { here we could reset R_EBX
  996. but that is risky because it only works
  997. if genexitcode is called after genentrycode
  998. so lets skip this for the moment PM
  999. aktprocsym^.definition^.usedregisters:=
  1000. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  1001. }
  1002. end;
  1003. {$endif dummy}
  1004. if not(nostackframe) and not inlined then
  1005. g_restore_frame_pointer(list);
  1006. { at last, the return is generated }
  1007. if not inlined then
  1008. {$IFDEF NEWST}
  1009. if pointerrupt in aktprocdef^.options then
  1010. {$ELSE}
  1011. if po_interrupt in aktprocsym^.definition^.procoptions then
  1012. {$ENDIF NEWST}
  1013. g_interrupt_stackframe_exit(list)
  1014. else
  1015. g_return_from_proc(list,parasize);
  1016. {$IFDEF NEWST}
  1017. list^.concat(new(pai_symbol_end,initname(aktprocdef^.mangledname)));
  1018. {$ELSE NEWST}
  1019. list^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  1020. {$ENDIF NEWST}
  1021. {$ifdef GDB}
  1022. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  1023. begin
  1024. aktprocsym^.concatstabto(list);
  1025. if assigned(procinfo^._class) then
  1026. if (not assigned(procinfo^.parent) or
  1027. not assigned(procinfo^.parent^._class)) then
  1028. list^.concat(new(pai_stabs,init(strpnew(
  1029. '"$t:v'+procinfo^._class^.numberstring+'",'+
  1030. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset)))));
  1031. {!!!!!!!!!!!!
  1032. else
  1033. list^.concat(new(pai_stabs,init(strpnew(
  1034. '"$t:r'+procinfo^._class^.numberstring+'",'+
  1035. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  1036. }
  1037. if (tdef(aktprocsym^.definition^.rettype.def) <> tdef(voiddef)) then
  1038. begin
  1039. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  1040. list^.concat(new(pai_stabs,init(strpnew(
  1041. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  1042. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  1043. else
  1044. list^.concat(new(pai_stabs,init(strpnew(
  1045. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  1046. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  1047. if (m_result in aktmodeswitches) then
  1048. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  1049. list^.concat(new(pai_stabs,init(strpnew(
  1050. '"RESULT:X*'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  1051. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  1052. else
  1053. list^.concat(new(pai_stabs,init(strpnew(
  1054. '"RESULT:X'+aktprocsym^.definition^.rettype.def^.numberstring+'",'+
  1055. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  1056. end;
  1057. mangled_length:=length(aktprocsym^.definition^.mangledname);
  1058. getmem(p,mangled_length+50);
  1059. strpcopy(p,'192,0,0,');
  1060. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  1061. list^.concat(new(pai_stabn,init(strnew(p))));
  1062. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  1063. +aktprocsym^.definition^.mangledname))));
  1064. p[0]:='2';p[1]:='2';p[2]:='4';
  1065. strpcopy(strend(p),'_end');}
  1066. freemem(p,mangled_length+50);
  1067. list^.concat(new(pai_stabn,init(
  1068. strpnew('224,0,0,'+aktexit2label^.name))));
  1069. { strpnew('224,0,0,'
  1070. +aktprocsym^.definition^.mangledname+'_end'))));}
  1071. end;
  1072. {$endif GDB}
  1073. *)
  1074. runerror(211);
  1075. end;
  1076. {*****************************************************************************
  1077. some generic implementations
  1078. ****************************************************************************}
  1079. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aword;const ref : treference);
  1080. var
  1081. tmpreg: tregister;
  1082. begin
  1083. tmpreg := get_scratch_reg(list);
  1084. a_load_const_reg(list,size,a,tmpreg);
  1085. a_load_reg_ref(list,size,tmpreg,ref);
  1086. free_scratch_reg(list,tmpreg);
  1087. end;
  1088. procedure tcg.a_load_loc_reg(list : taasmoutput;size : tcgsize;const loc: tlocation; reg : tregister);
  1089. begin
  1090. case loc.loc of
  1091. LOC_REFERENCE,LOC_MEM:
  1092. a_load_ref_reg(list,size,loc.reference,reg);
  1093. LOC_REGISTER,LOC_CREGISTER:
  1094. a_load_reg_reg(list,size,loc.register,reg);
  1095. else
  1096. internalerror(200109092);
  1097. end;
  1098. end;
  1099. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  1100. var
  1101. tmpreg: tregister;
  1102. begin
  1103. tmpreg := get_scratch_reg(list);
  1104. a_load_ref_reg(list,size,ref,tmpreg);
  1105. a_op_const_reg(list,op,a,tmpreg);
  1106. a_load_reg_ref(list,size,tmpreg,ref);
  1107. free_scratch_reg(list,tmpreg);
  1108. end;
  1109. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const loc: tlocation);
  1110. begin
  1111. case loc.loc of
  1112. LOC_REGISTER, LOC_CREGISTER:
  1113. a_op_const_reg(list,op,a,loc.register);
  1114. LOC_REFERENCE, LOC_MEM:
  1115. a_op_const_ref(list,op,size,a,loc.reference);
  1116. else
  1117. internalerror(200109061);
  1118. end;
  1119. end;
  1120. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1121. var
  1122. tmpreg: tregister;
  1123. begin
  1124. tmpreg := get_scratch_reg(list);
  1125. a_load_ref_reg(list,size,ref,tmpreg);
  1126. a_op_reg_reg(list,op,size,reg,tmpreg);
  1127. a_load_reg_ref(list,size,tmpreg,ref);
  1128. free_scratch_reg(list,tmpreg);
  1129. end;
  1130. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1131. var
  1132. tmpreg: tregister;
  1133. begin
  1134. case op of
  1135. OP_NOT,OP_NEG:
  1136. { handle it as "load ref,reg; op reg" }
  1137. begin
  1138. a_load_ref_reg(list,size,ref,reg);
  1139. a_op_reg_reg(list,op,size,reg,reg);
  1140. end;
  1141. else
  1142. begin
  1143. tmpreg := get_scratch_reg(list);
  1144. a_load_ref_reg(list,size,ref,tmpreg);
  1145. a_op_reg_reg(list,op,size,tmpreg,reg);
  1146. free_scratch_reg(list,tmpreg);
  1147. end;
  1148. end;
  1149. end;
  1150. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: tregister; const loc: tlocation);
  1151. begin
  1152. case loc.loc of
  1153. LOC_REGISTER, LOC_CREGISTER:
  1154. a_op_reg_reg(list,op,size,reg,loc.register);
  1155. LOC_REFERENCE, LOC_MEM:
  1156. a_op_reg_ref(list,op,size,reg,loc.reference);
  1157. else
  1158. internalerror(200109061);
  1159. end;
  1160. end;
  1161. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; const loc: tlocation);
  1162. var
  1163. tmpreg: tregister;
  1164. begin
  1165. case loc.loc of
  1166. LOC_REGISTER,LOC_CREGISTER:
  1167. a_op_ref_reg(list,op,size,ref,loc.register);
  1168. LOC_REFERENCE,LOC_MEM:
  1169. begin
  1170. tmpreg := get_scratch_reg(list);
  1171. {$ifdef i386}
  1172. makeregsize(tmpreg,size);
  1173. {$endif i386}
  1174. a_load_ref_reg(list,size,ref,tmpreg);
  1175. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  1176. free_scratch_reg(list,tmpreg);
  1177. end;
  1178. else
  1179. internalerror(200109061);
  1180. end;
  1181. end;
  1182. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  1183. l : tasmlabel);
  1184. var
  1185. tmpreg: tregister;
  1186. begin
  1187. tmpreg := get_scratch_reg(list);
  1188. a_load_ref_reg(list,size,ref,tmpreg);
  1189. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1190. free_scratch_reg(list,tmpreg);
  1191. end;
  1192. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const loc : tlocation;
  1193. l : tasmlabel);
  1194. begin
  1195. case loc.loc of
  1196. LOC_REGISTER,LOC_CREGISTER:
  1197. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1198. LOC_REFERENCE,LOC_MEM:
  1199. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1200. else
  1201. internalerror(200109061);
  1202. end;
  1203. end;
  1204. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1205. var
  1206. tmpreg: tregister;
  1207. begin
  1208. tmpreg := get_scratch_reg(list);
  1209. a_load_ref_reg(list,size,ref,tmpreg);
  1210. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1211. free_scratch_reg(list,tmpreg);
  1212. end;
  1213. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1214. l : tasmlabel);
  1215. var
  1216. tmpreg: tregister;
  1217. begin
  1218. case loc.loc of
  1219. LOC_REGISTER,LOC_CREGISTER:
  1220. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1221. LOC_REFERENCE,LOC_MEM:
  1222. begin
  1223. {$ifdef i386}
  1224. { the following is done with defines to avoid a speed penalty, }
  1225. { since all this is only necessary for the 80x86 (because EDI }
  1226. { doesn't have an 8bit component which is directly addressable) }
  1227. if size in [OS_8,OS_S8] then
  1228. tmpreg := getregister32
  1229. else
  1230. {$endif i386}
  1231. tmpreg := get_scratch_reg(list);
  1232. {$ifdef i386}
  1233. makeregsize(tmpreg,size);
  1234. {$endif i386}
  1235. a_load_ref_reg(list,size,loc.reference,tmpreg);
  1236. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1237. {$ifdef i386}
  1238. if makereg32(tmpreg) <> R_EDI then
  1239. ungetregister(tmpreg)
  1240. else
  1241. {$endif i386}
  1242. free_scratch_reg(list,tmpreg);
  1243. end
  1244. else
  1245. internalerror(200109061);
  1246. end;
  1247. end;
  1248. finalization
  1249. cg.free;
  1250. end.
  1251. {
  1252. $Log$
  1253. Revision 1.2 2001-09-28 20:39:32 jonas
  1254. * changed all flow control structures (except for exception handling
  1255. related things) to processor independent code (in new ncgflw unit)
  1256. + generic cgobj unit which contains lots of code generator helpers with
  1257. global "cg" class instance variable
  1258. + cgcpu unit for i386 (implements processor specific routines of the above
  1259. unit)
  1260. * updated cgbase and cpubase for the new code generator units
  1261. * include ncgflw unit in cpunode unit
  1262. Revision 1.5 2001/09/09 17:10:26 jonas
  1263. * some more things implemented
  1264. Revision 1.4 2001/09/06 15:25:55 jonas
  1265. * changed type of tcg from object to class -> abstract methods are now
  1266. a lot cleaner :)
  1267. + more updates: load_*_loc methods, op_*_* methods, g_flags2reg method
  1268. (if possible with geenric implementation and necessary ppc
  1269. implementations)
  1270. * worked a bit further on cgflw, now working on exitnode
  1271. Revision 1.3 2001/09/05 20:21:03 jonas
  1272. * new cgflow based on n386flw with all nodes until forn "translated"
  1273. + a_cmp_loc_*_label methods for tcg
  1274. + base implementatino for a_cmp_ref_*_label methods
  1275. * small bugfixes to powerpc cg
  1276. Revision 1.2 2001/08/26 13:37:04 florian
  1277. * some cg reorganisation
  1278. * some PPC updates
  1279. Revision 1.1 2000/07/13 06:30:07 michael
  1280. + Initial import
  1281. }