cgobj.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. {
  2. $Id$
  3. Copyright (c) 1993-99 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. interface
  21. uses
  22. cobjects,aasm,symtable,symconst,cpuasm,cpubase,cgbase,cpuinfo;
  23. type
  24. qword = comp;
  25. pcg = ^tcg;
  26. tcg = object
  27. constructor init;
  28. destructor done;virtual;
  29. procedure a_call_name_ext(list : paasmoutput;const s : string;
  30. offset : longint);
  31. {************************************************}
  32. { code generation for subroutine entry/exit code }
  33. { helper routines }
  34. procedure g_initialize_data(list : paasmoutput;p : psym);
  35. procedure g_incr_data(list : paasmoutput;p : psym);
  36. procedure g_finalize_data(list : paasmoutput;p : pnamedindexobject);
  37. procedure g_copyvalueparas(list : paasmoutput;p : pnamedindexobject);
  38. procedure g_finalizetempansistrings(list : paasmoutput);
  39. { finalizes data of type t }
  40. { if is_already_ref is true then the routines assumes }
  41. { that r points to the data to finalizes }
  42. procedure g_finalize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  43. procedure g_entrycode(list : paasmoutput;
  44. const proc_names : tstringcontainer;make_global : boolean;
  45. stackframe : longint;var parasize : longint;
  46. var nostackframe : boolean;inlined : boolean);
  47. procedure g_exitcode(list : paasmoutput;parasize : longint;
  48. nostackframe,inlined : boolean);
  49. { string helper routines }
  50. procedure g_decrstrref(list : paasmoutput;const ref : treference;t : pdef);
  51. procedure g_removetemps(list : paasmoutput;p : plinkedlist);
  52. {**********************************}
  53. { these methods must be overriden: }
  54. { Remarks:
  55. * If a method specifies a size you have only to take care
  56. of that number of bits, i.e. load_const_reg with OP_8 must
  57. only load the lower 8 bit of the specified register
  58. the rest of the register can be undefined
  59. if necessary the compiler will call a method
  60. to zero or sign extend the register
  61. * The a_load_XX_XX with OP_64 needn't to be
  62. implemented for 32 bit
  63. processors, the code generator takes care of that
  64. * the addr size is for work with the natural pointer
  65. size
  66. * the procedures without fpu/mm are only for integer usage
  67. * normally the first location is the source and the
  68. second the destination
  69. }
  70. procedure a_call_name(list : paasmoutput;const s : string;
  71. offset : longint);virtual;
  72. { move instructions }
  73. procedure a_load_const_reg(list : paasmoutput;size : tcgsize;a : aword;register : tregister);virtual;
  74. procedure a_load_reg_ref(list : paasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual;
  75. procedure a_load_ref_reg(list : paasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual;
  76. procedure a_load_reg_reg(list : paasmoutput;size : tcgsize;reg1,reg2 : tregister);virtual;
  77. { comparison operations }
  78. procedure a_cmp_reg_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;b : byte;reg : tregister;
  79. l : pasmlabel);virtual;
  80. procedure a_cmp_reg_reg_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : pasmlabel);
  81. procedure a_cmp_reg_ref_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister;l : pasmlabel);
  82. procedure a_cmp_ref_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  83. l : pasmlabel);
  84. procedure a_loadaddress_ref_reg(list : paasmoutput;const ref : treference;r : tregister);virtual;
  85. procedure g_stackframe_entry(list : paasmoutput;localsize : longint);virtual;
  86. procedure g_maybe_loadself(list : paasmoutput);virtual;
  87. { restores the frame pointer at procedure exit, for the }
  88. { i386 it generates a simple leave }
  89. procedure g_restore_frame_pointer(list : paasmoutput);virtual;
  90. { some processors like the PPC doesn't allow to change the stack in }
  91. { a procedure, so we need to maintain an extra stack for the }
  92. { result values of setjmp in exception code }
  93. { this two procedures are for pushing an exception value, }
  94. { they can use the scratch registers }
  95. procedure g_push_exception_value_reg(list : paasmoutput;reg : tregister);virtual;
  96. procedure g_push_exception_value_const(list : paasmoutput;reg : tregister);virtual;
  97. { that procedure pops a exception value }
  98. procedure g_pop_exception_value_reg(list : paasmoutput;reg : tregister);virtual;
  99. procedure g_return_from_proc(list : paasmoutput;parasize : aword);virtual;
  100. {********************************************************}
  101. { these methods can be overriden for extra functionality }
  102. { the following methods do nothing: }
  103. procedure g_interrupt_stackframe_entry(list : paasmoutput);virtual;
  104. procedure g_interrupt_stackframe_exit(list : paasmoutput);virtual;
  105. procedure g_profilecode(list : paasmoutput);virtual;
  106. procedure g_stackcheck(list : paasmoutput;stackframesize : longint);virtual;
  107. procedure a_load_const_ref(list : paasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  108. { passing parameters, per default the parameter is pushed }
  109. { nr gives the number of the parameter (enumerated from }
  110. { left to right), this allows to move the parameter to }
  111. { register, if the cpu supports register calling }
  112. { conventions }
  113. procedure a_param_reg(list : paasmoutput;size : tcgsize;r : tregister;nr : longint);virtual;
  114. procedure a_param_const(list : paasmoutput;size : tcgsize;a : aword;nr : longint);virtual;
  115. procedure a_param_ref(list : paasmoutput;size : tcgsize;const r : treference;nr : longint);virtual;
  116. procedure a_paramaddr_ref(list : paasmoutput;const r : treference;nr : longint);virtual;
  117. { uses the addr of ref as param, was emitpushreferenceaddr }
  118. procedure a_param_ref_addr(list : paasmoutput;r : treference;nr : longint);virtual;
  119. procedure a_label(list : paasmoutput;l : pasmlabel);virtual;
  120. { allocates register r by inserting a pai_realloc record }
  121. procedure a_reg_alloc(list : paasmoutput;r : tregister);
  122. { deallocates register r by inserting a pa_regdealloc record}
  123. procedure a_reg_dealloc(list : paasmoutput;r : tregister);
  124. end;
  125. var
  126. cg : pcg; { this is the main code generator class }
  127. implementation
  128. uses
  129. globals,globtype,options,files,gdb,systems,
  130. ppu,verbose,types,tgobj,tgcpu;
  131. {*****************************************************************************
  132. basic functionallity
  133. ******************************************************************************}
  134. constructor tcg.init;
  135. begin
  136. end;
  137. destructor tcg.done;
  138. begin
  139. end;
  140. procedure tcg.a_reg_alloc(list : paasmoutput;r : tregister);
  141. begin
  142. list^.concat(new(pairegalloc,alloc(r)));
  143. end;
  144. procedure tcg.a_reg_dealloc(list : paasmoutput;r : tregister);
  145. begin
  146. list^.concat(new(pairegalloc,dealloc(r)));
  147. end;
  148. procedure tcg.a_label(list : paasmoutput;l : pasmlabel);
  149. begin
  150. list^.concat(new(pai_label,init(l)));
  151. end;
  152. {*****************************************************************************
  153. this methods must be overridden for extra functionality
  154. ******************************************************************************}
  155. procedure tcg.g_interrupt_stackframe_entry(list : paasmoutput);
  156. begin
  157. end;
  158. procedure tcg.g_interrupt_stackframe_exit(list : paasmoutput);
  159. begin
  160. end;
  161. procedure tcg.g_profilecode(list : paasmoutput);
  162. begin
  163. end;
  164. {*****************************************************************************
  165. for better code generation these methods should be overridden
  166. ******************************************************************************}
  167. procedure tcg.a_param_const(list : paasmoutput;size : tcgsize;a : aword;nr : longint);
  168. begin
  169. a_reg_alloc(list,scratch_register);
  170. a_load_const_reg(list,size,a,scratch_register);
  171. a_param_reg(list,size,scratch_register,nr);
  172. a_reg_dealloc(list,scratch_register);
  173. end;
  174. procedure tcg.a_param_ref(list : paasmoutput;size : tcgsize;const r : treference;nr : longint);
  175. begin
  176. a_reg_alloc(list,scratch_register);
  177. a_load_ref_reg(list,size,r,scratch_register);
  178. a_param_reg(list,size,scratch_register,nr);
  179. a_reg_dealloc(list,scratch_register);
  180. end;
  181. procedure tcg.a_param_ref_addr(list : paasmoutput;r : treference;nr : longint);
  182. begin
  183. a_reg_alloc(list,scratch_register);
  184. a_loadaddress_ref_reg(list,r,scratch_register);
  185. a_param_reg(list,OS_ADDR,scratch_register,nr);
  186. a_reg_dealloc(list,scratch_register);
  187. end;
  188. procedure tcg.g_stackcheck(list : paasmoutput;stackframesize : longint);
  189. begin
  190. a_param_const(list,OS_32,stackframesize,1);
  191. a_call_name_ext(list,'FPC_STACKCHECK',0);
  192. end;
  193. procedure tcg.a_call_name_ext(list : paasmoutput;const s : string;
  194. offset : longint);
  195. begin
  196. a_call_name(list,s,offset);
  197. end;
  198. procedure tcg.a_load_const_ref(list : paasmoutput;size : tcgsize;a : aword;const ref : treference);
  199. begin
  200. a_reg_alloc(list,scratch_register);
  201. a_load_const_reg(list,size,a,scratch_register);
  202. a_load_reg_ref(list,size,scratch_register,ref);
  203. a_reg_dealloc(list,scratch_register);
  204. end;
  205. {*****************************************************************************
  206. String helper routines
  207. *****************************************************************************}
  208. procedure tcg.g_removetemps(list : paasmoutput;p : plinkedlist);
  209. var
  210. hp : ptemptodestroy;
  211. pushedregs : tpushed;
  212. begin
  213. hp:=ptemptodestroy(p^.first);
  214. if not(assigned(hp)) then
  215. exit;
  216. tg.pushusedregisters(pushedregs,$ff);
  217. while assigned(hp) do
  218. begin
  219. if is_ansistring(hp^.typ) then
  220. begin
  221. g_decrstrref(list,hp^.address,hp^.typ);
  222. tg.ungetiftemp(hp^.address);
  223. end;
  224. hp:=ptemptodestroy(hp^.next);
  225. end;
  226. tg.popusedregisters(pushedregs);
  227. end;
  228. procedure tcg.g_decrstrref(list : paasmoutput;const ref : treference;t : pdef);
  229. var
  230. pushedregs : tpushed;
  231. begin
  232. tg.pushusedregisters(pushedregs,$ff);
  233. a_param_ref_addr(list,ref,1);
  234. if is_ansistring(t) then
  235. a_call_name(list,'FPC_ANSISTR_DECR_REF',0)
  236. else if is_widestring(t) then
  237. a_call_name(list,'FPC_WIDESTR_DECR_REF',0)
  238. else internalerror(58993);
  239. tg.popusedregisters(pushedregs);
  240. end;
  241. {*****************************************************************************
  242. Code generation for subroutine entry- and exit code
  243. *****************************************************************************}
  244. procedure tcg.g_finalize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  245. var
  246. r : treference;
  247. begin
  248. if is_ansistring(t) or
  249. is_widestring(t) then
  250. begin
  251. g_decrstrref(list,ref,t);
  252. end
  253. else
  254. begin
  255. reset_reference(r);
  256. r.symbol:=t^.get_inittable_label;
  257. a_param_ref_addr(list,r,2);
  258. if is_already_ref then
  259. a_paramaddr_ref(list,ref,1)
  260. else
  261. a_param_ref_addr(list,ref,1);
  262. a_call_name(list,'FPC_FINALIZE',0);
  263. end;
  264. end;
  265. { generates the code for initialisation of local data }
  266. procedure tcg.g_initialize_data(list : paasmoutput;p : psym);
  267. begin
  268. runerror(255);
  269. end;
  270. { generates the code for incrementing the reference count of parameters }
  271. procedure tcg.g_incr_data(list : paasmoutput;p : psym);
  272. var
  273. hr : treference;
  274. begin
  275. if (psym(p)^.typ=varsym) and
  276. not((pvarsym(p)^.definition^.deftype=objectdef) and
  277. pobjectdef(pvarsym(p)^.definition)^.is_class) and
  278. pvarsym(p)^.definition^.needs_inittable and
  279. ((pvarsym(p)^.varspez=vs_value)) then
  280. begin
  281. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  282. reset_reference(hr);
  283. hr.symbol:=pvarsym(p)^.definition^.get_inittable_label;
  284. a_param_ref_addr(list,hr,2);
  285. reset_reference(hr);
  286. hr.base:=procinfo.framepointer;
  287. hr.offset:=pvarsym(p)^.address+procinfo.call_offset;
  288. a_param_ref_addr(list,hr,1);
  289. reset_reference(hr);
  290. a_call_name(list,'FPC_ADDREF',0);
  291. end;
  292. end;
  293. { generates the code for finalisation of local data }
  294. procedure tcg.g_finalize_data(list : paasmoutput;p : pnamedindexobject);
  295. begin
  296. runerror(255);
  297. end;
  298. { generates the code to make local copies of the value parameters }
  299. procedure tcg.g_copyvalueparas(list : paasmoutput;p : pnamedindexobject);
  300. begin
  301. runerror(255);
  302. end;
  303. var
  304. _list : paasmoutput;
  305. { wrappers for the methods, because TP doesn't know procedures }
  306. { of objects }
  307. procedure _copyvalueparas(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  308. begin
  309. cg^.g_copyvalueparas(_list,s);
  310. end;
  311. procedure tcg.g_finalizetempansistrings(list : paasmoutput);
  312. var
  313. hp : ptemprecord;
  314. hr : treference;
  315. begin
  316. hp:=tg.templist;
  317. while assigned(hp) do
  318. begin
  319. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  320. begin
  321. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  322. reset_reference(hr);
  323. hr.base:=procinfo.framepointer;
  324. hr.offset:=hp^.pos;
  325. a_param_ref_addr(list,hr,1);
  326. a_call_name(list,'FPC_ANSISTR_DECR_REF',0);
  327. end;
  328. hp:=hp^.next;
  329. end;
  330. end;
  331. procedure _finalize_data(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  332. begin
  333. cg^.g_finalize_data(_list,s);
  334. end;
  335. procedure _incr_data(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  336. begin
  337. cg^.g_incr_data(_list,psym(s));
  338. end;
  339. procedure _initialize_data(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  340. begin
  341. cg^.g_initialize_data(_list,psym(s));
  342. end;
  343. { generates the entry code for a procedure }
  344. procedure tcg.g_entrycode(list : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  345. stackframe:longint;var parasize:longint;var nostackframe:boolean;
  346. inlined : boolean);
  347. var
  348. hs : string;
  349. hp : pused_unit;
  350. initcode : taasmoutput;
  351. {$ifdef GDB}
  352. stab_function_name : Pai_stab_function_name;
  353. {$endif GDB}
  354. hr : treference;
  355. r : tregister;
  356. begin
  357. { Align }
  358. if (not inlined) then
  359. begin
  360. { gprof uses 16 byte granularity !! }
  361. if (cs_profile in aktmoduleswitches) then
  362. list^.insert(new(pai_align,init(16)))
  363. else
  364. if not(cs_littlesize in aktglobalswitches) then
  365. list^.insert(new(pai_align,init(4)));
  366. end;
  367. { save registers on cdecl }
  368. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  369. begin
  370. for r:=firstreg to lastreg do
  371. begin
  372. if (r in registers_saved_on_cdecl) then
  373. if (r in (tg.availabletempregsint+
  374. tg.availabletempregsfpu+
  375. tg.availabletempregsmm)) then
  376. begin
  377. if not(r in tg.usedinproc) then
  378. {!!!!!!!!!!!! a_push_reg(list,r) }
  379. end
  380. else
  381. {!!!!!!!! a_push_reg(list,r) };
  382. end;
  383. end;
  384. { omit stack frame ? }
  385. if not inlined then
  386. if procinfo.framepointer=stack_pointer then
  387. begin
  388. CGMessage(cg_d_stackframe_omited);
  389. nostackframe:=true;
  390. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  391. parasize:=0
  392. else
  393. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-pointersize;
  394. end
  395. else
  396. begin
  397. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  398. parasize:=0
  399. else
  400. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-pointersize*2;
  401. nostackframe:=false;
  402. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  403. g_interrupt_stackframe_entry(list);
  404. g_stackframe_entry(list,stackframe);
  405. if (cs_check_stack in aktlocalswitches) and
  406. (tf_supports_stack_checking in target_info.flags) then
  407. g_stackcheck(@initcode,stackframe);
  408. end;
  409. if cs_profile in aktmoduleswitches then
  410. g_profilecode(@initcode);
  411. if (not inlined) and (aktprocsym^.definition^.proctypeoption in [potype_unitinit]) then
  412. begin
  413. { needs the target a console flags ? }
  414. if tf_needs_isconsole in target_info.flags then
  415. begin
  416. hr.symbol:=newasmsymbol('U_'+target_info.system_unit+'_ISCONSOLE');
  417. if apptype=at_cui then
  418. a_load_const_ref(list,OS_8,1,hr)
  419. else
  420. a_load_const_ref(list,OS_8,0,hr);
  421. dispose(hr.symbol,done);
  422. end;
  423. hp:=pused_unit(usedunits.first);
  424. while assigned(hp) do
  425. begin
  426. { call the unit init code and make it external }
  427. if (hp^.u^.flags and uf_init)<>0 then
  428. a_call_name_ext(list,
  429. 'INIT$$'+hp^.u^.modulename^,0);
  430. hp:=Pused_unit(hp^.next);
  431. end;
  432. end;
  433. {$ifdef dummy}
  434. { a constructor needs a help procedure }
  435. if (aktprocsym^.definition^.options and poconstructor)<>0 then
  436. begin
  437. if procinfo._class^.isclass then
  438. begin
  439. list^.concat(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_NEW_CLASS'))));
  440. list^.concat(new(pai386,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  441. end
  442. else
  443. begin
  444. {
  445. list^.insert(new(pai_labeled,init(A_JZ,quickexitlabel)));
  446. list^.insert(new(pai386,op_csymbol(A_CALL,S_NO,
  447. newcsymbol('FPC_HELP_CONSTRUCTOR',0))));
  448. list^.insert(new(pai386,op_const_reg(A_MOV,S_L,procinfo._class^.vmt_offset,R_EDI)));
  449. concat_external('FPC_HELP_CONSTRUCTOR',EXT_NEAR);
  450. }
  451. end;
  452. end;
  453. {$endif dummy}
  454. {$ifdef GDB}
  455. if (cs_debuginfo in aktmoduleswitches) then
  456. list^.insert(new(pai_force_line,init));
  457. {$endif GDB}
  458. { initialize return value }
  459. if is_ansistring(procinfo.retdef) or
  460. is_widestring(procinfo.retdef) then
  461. begin
  462. reset_reference(hr);
  463. hr.offset:=procinfo.retoffset;
  464. hr.base:=procinfo.framepointer;
  465. a_load_const_ref(list,OS_32,0,hr);
  466. end;
  467. _list:=list;
  468. { generate copies of call by value parameters }
  469. if (po_assembler in aktprocsym^.definition^.procoptions) then
  470. aktprocsym^.definition^.parast^.foreach({$ifdef FPC}@{$endif FPC}_copyvalueparas);
  471. { initialisizes local data }
  472. aktprocsym^.definition^.localst^.foreach({$ifdef FPC}@{$endif FPC}_initialize_data);
  473. { add a reference to all call by value/const parameters }
  474. aktprocsym^.definition^.parast^.foreach({$ifdef FPC}@{$endif FPC}_incr_data);
  475. if (cs_profile in aktmoduleswitches) or
  476. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  477. (assigned(procinfo._class) and (procinfo._class^.owner^.symtabletype=globalsymtable)) then
  478. make_global:=true;
  479. if not inlined then
  480. begin
  481. hs:=proc_names.get;
  482. {$ifdef GDB}
  483. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  484. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  485. {$endif GDB}
  486. { insert the names for the procedure }
  487. while hs<>'' do
  488. begin
  489. if make_global then
  490. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  491. else
  492. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  493. {$ifdef GDB}
  494. if (cs_debuginfo in aktmoduleswitches) then
  495. begin
  496. if target_os.use_function_relative_addresses then
  497. list^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  498. end;
  499. {$endif GDB}
  500. hs:=proc_names.get;
  501. end;
  502. end;
  503. {$ifdef GDB}
  504. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  505. begin
  506. if target_os.use_function_relative_addresses then
  507. list^.insert(stab_function_name);
  508. if make_global or ((procinfo.flags and pi_is_global) <> 0) then
  509. aktprocsym^.is_global := True;
  510. list^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  511. aktprocsym^.isstabwritten:=true;
  512. end;
  513. {$endif GDB}
  514. end;
  515. procedure tcg.g_exitcode(list : paasmoutput;parasize:longint;nostackframe,inlined:boolean);
  516. var
  517. {$ifdef GDB}
  518. mangled_length : longint;
  519. p : pchar;
  520. {$endif GDB}
  521. noreraiselabel : pasmlabel;
  522. hr : treference;
  523. begin
  524. if aktexitlabel^.is_used then
  525. list^.insert(new(pai_label,init(aktexitlabel)));
  526. { call the destructor help procedure }
  527. if (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  528. begin
  529. if procinfo._class^.is_class then
  530. a_call_name(list,'FPC_DISPOSE_CLASS',0)
  531. else
  532. begin
  533. a_reg_alloc(list,scratch_register);
  534. a_load_const_reg(list,OS_32,procinfo._class^.vmt_offset,scratch_register);
  535. a_call_name(list,'FPC_HELP_DESTRUCTOR',0);
  536. a_reg_dealloc(list,scratch_register);
  537. end;
  538. end;
  539. { finalize temporary data }
  540. g_finalizetempansistrings(list);
  541. _list:=list;
  542. { finalize local data }
  543. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}_finalize_data);
  544. { finalize paras data }
  545. if assigned(aktprocsym^.definition^.parast) then
  546. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}_finalize_data);
  547. { do we need to handle exceptions because of ansi/widestrings ? }
  548. if (procinfo.flags and pi_needs_implicit_finally)<>0 then
  549. begin
  550. getlabel(noreraiselabel);
  551. a_call_name(list,'FPC_POPADDRSTACK',0);
  552. a_reg_alloc(list,accumulator);
  553. g_pop_exception_value_reg(list,accumulator);
  554. a_cmp_reg_const_label(list,OS_32,OC_EQ,0,accumulator,noreraiselabel);
  555. a_reg_dealloc(list,accumulator);
  556. { must be the return value finalized before reraising the exception? }
  557. if (procinfo.retdef<>pdef(voiddef)) and
  558. (procinfo.retdef^.needs_inittable) and
  559. ((procinfo.retdef^.deftype<>objectdef) or
  560. not(pobjectdef(procinfo.retdef)^.is_class)) then
  561. begin
  562. reset_reference(hr);
  563. hr.offset:=procinfo.retoffset;
  564. hr.base:=procinfo.framepointer;
  565. g_finalize(list,procinfo.retdef,hr,ret_in_param(procinfo.retdef));
  566. end;
  567. a_call_name(list,'FPC_RERAISE',0);
  568. a_label(list,noreraiselabel);
  569. end;
  570. { call __EXIT for main program }
  571. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  572. a_call_name(list,'FPC_DO_EXIT',0);
  573. { handle return value }
  574. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  575. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  576. { handle_return_value(inlined) }
  577. else
  578. begin
  579. { return self in EAX }
  580. a_label(list,quickexitlabel);
  581. a_reg_alloc(list,accumulator);
  582. a_load_reg_reg(list,OS_ADDR,self_pointer,accumulator);
  583. a_reg_dealloc(list,self_pointer);
  584. a_label(list,quickexitlabel);
  585. { we can't clear the zero flag because the Alpha }
  586. { for example doesn't have flags, we have to compare }
  587. { the accu. in the caller }
  588. end;
  589. { stabs uses the label also ! }
  590. if aktexit2label^.is_used or
  591. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  592. a_label(list,aktexit2label);
  593. {$ifdef dummy}
  594. { should we restore edi ? }
  595. { for all i386 gcc implementations }
  596. {!!!!!!!!!!! I don't know how to handle register saving yet }
  597. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  598. begin
  599. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  600. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  601. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  602. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  603. { here we could reset R_EBX
  604. but that is risky because it only works
  605. if genexitcode is called after genentrycode
  606. so lets skip this for the moment PM
  607. aktprocsym^.definition^.usedregisters:=
  608. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  609. }
  610. end;
  611. {$endif dummy}
  612. if not(nostackframe) and not inlined then
  613. g_restore_frame_pointer(list);
  614. { at last, the return is generated }
  615. if not inlined then
  616. if po_interrupt in aktprocsym^.definition^.procoptions then
  617. g_interrupt_stackframe_exit(list)
  618. else
  619. g_return_from_proc(list,parasize);
  620. list^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  621. {$ifdef GDB}
  622. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  623. begin
  624. aktprocsym^.concatstabto(exprasmlist);
  625. if assigned(procinfo._class) then
  626. if (not assigned(procinfo.parent) or
  627. not assigned(procinfo.parent^._class)) then
  628. list^.concat(new(pai_stabs,init(strpnew(
  629. '"$t:v'+procinfo._class^.numberstring+'",'+
  630. tostr(N_PSYM)+',0,0,'+tostr(procinfo.selfpointer_offset)))))
  631. else
  632. list^.concat(new(pai_stabs,init(strpnew(
  633. '"$t:r'+procinfo._class^.numberstring+'",'+
  634. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  635. if (pdef(aktprocsym^.definition^.retdef) <> pdef(voiddef)) then
  636. if ret_in_param(aktprocsym^.definition^.retdef) then
  637. list^.concat(new(pai_stabs,init(strpnew(
  638. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  639. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))))
  640. else
  641. list^.concat(new(pai_stabs,init(strpnew(
  642. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  643. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))));
  644. mangled_length:=length(aktprocsym^.definition^.mangledname);
  645. getmem(p,mangled_length+50);
  646. strpcopy(p,'192,0,0,');
  647. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  648. exprasmlist^.concat(new(pai_stabn,init(strnew(p))));
  649. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  650. +aktprocsym^.definition^.mangledname))));
  651. p[0]:='2';p[1]:='2';p[2]:='4';
  652. strpcopy(strend(p),'_end');}
  653. freemem(p,mangled_length+50);
  654. exprasmlist^.concat(new(pai_stabn,init(
  655. strpnew('224,0,0,'+aktexit2label^.name))));
  656. { strpnew('224,0,0,'
  657. +aktprocsym^.definition^.mangledname+'_end'))));}
  658. end;
  659. {$endif GDB}
  660. end;
  661. {*****************************************************************************
  662. some abstract definitions
  663. ****************************************************************************}
  664. procedure tcg.a_call_name(list : paasmoutput;const s : string;
  665. offset : longint);
  666. begin
  667. abstract;
  668. end;
  669. procedure tcg.g_stackframe_entry(list : paasmoutput;localsize : longint);
  670. begin
  671. abstract;
  672. end;
  673. procedure tcg.g_maybe_loadself(list : paasmoutput);
  674. begin
  675. abstract;
  676. end;
  677. procedure tcg.g_restore_frame_pointer(list : paasmoutput);
  678. begin
  679. abstract;
  680. end;
  681. procedure g_return_from_proc(list : paasmoutput;parasize : aword);
  682. begin
  683. abstract;
  684. end;
  685. procedure tcg.a_loadaddress_ref_reg(list : paasmoutput;const ref : treference;r : tregister);
  686. begin
  687. abstract;
  688. end;
  689. procedure tcg.g_push_exception_value_reg(list : paasmoutput;reg : tregister);
  690. begin
  691. abstract;
  692. end;
  693. procedure tcg.g_push_exception_value_const(list : paasmoutput;reg : tregister);
  694. begin
  695. abstract;
  696. end;
  697. procedure tcg.g_pop_exception_value_reg(list : paasmoutput;reg : tregister);
  698. begin
  699. abstract;
  700. end;
  701. procedure tcg.a_load_const_reg(list : paasmoutput;size : tcgsize;a : aword;register : tregister);
  702. begin
  703. abstract;
  704. end;
  705. procedure tcg.a_load_reg_ref(list : paasmoutput;size : tcgsize;register : tregister;const ref : treference);
  706. begin
  707. abstract;
  708. end;
  709. procedure tcg.a_load_ref_reg(list : paasmoutput;size : tcgsize;const ref : treference;register : tregister);
  710. begin
  711. abstract;
  712. end;
  713. procedure tcg.a_load_reg_reg(list : paasmoutput;size : tcgsize;reg1,reg2 : tregister);
  714. begin
  715. abstract;
  716. end;
  717. procedure tcg.a_cmp_reg_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;b : byte;reg : tregister;
  718. l : pasmlabel);
  719. begin
  720. abstract;
  721. end;
  722. procedure tcg.a_cmp_reg_reg_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : pasmlabel);
  723. begin
  724. abstract;
  725. end;
  726. procedure tcg.a_cmp_reg_ref_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister;l : pasmlabel);
  727. begin
  728. abstract;
  729. end;
  730. procedure tcg.a_cmp_ref_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  731. l : pasmlabel);
  732. begin
  733. abstract;
  734. end;
  735. procedure tcg.g_return_from_proc(list : paasmoutput;parasize : aword);
  736. begin
  737. abstract;
  738. end;
  739. procedure tcg.a_param_reg(list : paasmoutput;size : tcgsize;r : tregister;nr : longint);
  740. begin
  741. abstract;
  742. end;
  743. procedure tcg.a_paramaddr_ref(list : paasmoutput;const r : treference;nr : longint);
  744. begin
  745. abstract;
  746. end;
  747. end.
  748. {
  749. $Log$
  750. Revision 1.14 1999-08-06 14:15:51 florian
  751. * made the alpha version compilable
  752. Revision 1.13 1999/08/06 13:26:50 florian
  753. * more changes ...
  754. Revision 1.12 1999/08/05 17:10:56 florian
  755. * some more additions, especially procedure
  756. exit code generation
  757. Revision 1.11 1999/08/05 14:58:11 florian
  758. * some fixes for the floating point registers
  759. * more things for the new code generator
  760. Revision 1.10 1999/08/04 00:23:52 florian
  761. * renamed i386asm and i386base to cpuasm and cpubase
  762. Revision 1.9 1999/08/02 23:13:21 florian
  763. * more changes to compile for the Alpha
  764. Revision 1.8 1999/08/02 17:14:07 florian
  765. + changed the temp. generator to an object
  766. Revision 1.7 1999/08/01 23:05:55 florian
  767. * changes to compile with FPC
  768. Revision 1.6 1999/08/01 18:22:33 florian
  769. * made it again compilable
  770. Revision 1.5 1999/01/23 23:29:46 florian
  771. * first running version of the new code generator
  772. * when compiling exceptions under Linux fixed
  773. Revision 1.4 1999/01/13 22:52:36 florian
  774. + YES, finally the new code generator is compilable, but it doesn't run yet :(
  775. Revision 1.3 1998/12/26 15:20:30 florian
  776. + more changes for the new version
  777. Revision 1.2 1998/12/15 22:18:55 florian
  778. * some code added
  779. Revision 1.1 1998/12/15 16:32:58 florian
  780. + first version, derived from old routines
  781. }