cgobj.pas 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. stack alignment !!!!!!
  2. {
  3. $Id$
  4. Copyright (c) 1993-99 by Florian Klaempfl
  5. Member of the Free Pascal development team
  6. This unit implements the basic code generator object
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit cgobj;
  21. interface
  22. uses
  23. cobjects,aasm,symtable,symconst,cpuasm,cpubase,cgbase,cpuinfo,tainst;
  24. type
  25. qword = comp;
  26. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  27. pcg = ^tcg;
  28. tcg = object
  29. scratch_register_array_pointer : aword;
  30. unusedscratchregisters : tregisterset;
  31. alignment : talignment;
  32. {************************************************}
  33. { basic routines }
  34. constructor init;
  35. destructor done;virtual;
  36. procedure a_label(list : paasmoutput;l : pasmlabel);virtual;
  37. { allocates register r by inserting a pai_realloc record }
  38. procedure a_reg_alloc(list : paasmoutput;r : tregister);
  39. { deallocates register r by inserting a pa_regdealloc record}
  40. procedure a_reg_dealloc(list : paasmoutput;r : tregister);
  41. { returns a register for use as scratch register }
  42. function get_scratch_reg(list : paasmoutput) : tregister;
  43. { releases a scratch register }
  44. procedure free_scratch_reg(list : paasmoutput;r : tregister);
  45. {************************************************}
  46. { code generation for subroutine entry/exit code }
  47. { initilizes data of type t }
  48. { if is_already_ref is true then the routines assumes }
  49. { that r points to the data to initialize }
  50. procedure g_initialize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  51. { finalizes data of type t }
  52. { if is_already_ref is true then the routines assumes }
  53. { that r points to the data to finalizes }
  54. procedure g_finalize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  55. { helper routines }
  56. procedure g_initialize_data(list : paasmoutput;p : psym);
  57. procedure g_incr_data(list : paasmoutput;p : psym);
  58. procedure g_finalize_data(list : paasmoutput;p : pnamedindexobject);
  59. procedure g_copyvalueparas(list : paasmoutput;p : pnamedindexobject);
  60. procedure g_finalizetempansistrings(list : paasmoutput);
  61. procedure g_entrycode(list : paasmoutput;
  62. const proc_names : tstringcontainer;make_global : boolean;
  63. stackframe : longint;var parasize : longint;
  64. var nostackframe : boolean;inlined : boolean);
  65. procedure g_exitcode(list : paasmoutput;parasize : longint;
  66. nostackframe,inlined : boolean);
  67. { string helper routines }
  68. procedure g_decrstrref(list : paasmoutput;const ref : treference;t : pdef);
  69. procedure g_removetemps(list : paasmoutput;p : plinkedlist);
  70. { passing parameters, per default the parameter is pushed }
  71. { nr gives the number of the parameter (enumerated from }
  72. { left to right), this allows to move the parameter to }
  73. { register, if the cpu supports register calling }
  74. { conventions }
  75. procedure a_param_reg(list : paasmoutput;size : tcgsize;r : tregister;nr : longint);virtual;
  76. procedure a_param_const(list : paasmoutput;size : tcgsize;a : aword;nr : longint);virtual;
  77. procedure a_param_ref(list : paasmoutput;size : tcgsize;const r : treference;nr : longint);virtual;
  78. procedure a_paramaddr_ref(list : paasmoutput;const r : treference;nr : longint);virtual;
  79. {**********************************}
  80. { these methods must be overriden: }
  81. { Remarks:
  82. * If a method specifies a size you have only to take care
  83. of that number of bits, i.e. load_const_reg with OP_8 must
  84. only load the lower 8 bit of the specified register
  85. the rest of the register can be undefined
  86. if necessary the compiler will call a method
  87. to zero or sign extend the register
  88. * The a_load_XX_XX with OP_64 needn't to be
  89. implemented for 32 bit
  90. processors, the code generator takes care of that
  91. * the addr size is for work with the natural pointer
  92. size
  93. * the procedures without fpu/mm are only for integer usage
  94. * normally the first location is the source and the
  95. second the destination
  96. }
  97. procedure a_call_name(list : paasmoutput;const s : string;
  98. offset : longint);virtual;
  99. { move instructions }
  100. procedure a_load_const_reg(list : paasmoutput;size : tcgsize;a : aword;register : tregister);virtual;
  101. procedure a_load_reg_ref(list : paasmoutput;size : tcgsize;register : tregister;const ref : treference);virtual;
  102. procedure a_load_ref_reg(list : paasmoutput;size : tcgsize;const ref : treference;register : tregister);virtual;
  103. procedure a_load_reg_reg(list : paasmoutput;size : tcgsize;reg1,reg2 : tregister);virtual;
  104. { comparison operations }
  105. procedure a_cmp_reg_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  106. l : pasmlabel);virtual;
  107. procedure a_cmp_reg_reg_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : pasmlabel);
  108. procedure a_cmp_reg_ref_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister;l : pasmlabel);
  109. procedure a_cmp_ref_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  110. l : pasmlabel);
  111. procedure a_jmp_cond(list : paasmoutput;cond : TOpCmp;l: pasmlabel);
  112. procedure a_loadaddress_ref_reg(list : paasmoutput;const ref : treference;r : tregister);virtual;
  113. procedure g_stackframe_entry(list : paasmoutput;localsize : longint);virtual;
  114. { restores the frame pointer at procedure exit, for the }
  115. { i386 it generates a simple leave }
  116. procedure g_restore_frame_pointer(list : paasmoutput);virtual;
  117. { some processors like the PPC doesn't allow to change the stack in }
  118. { a procedure, so we need to maintain an extra stack for the }
  119. { result values of setjmp in exception code }
  120. { this two procedures are for pushing an exception value, }
  121. { they can use the scratch registers }
  122. procedure g_push_exception_value_reg(list : paasmoutput;reg : tregister);virtual;
  123. procedure g_push_exception_value_const(list : paasmoutput;reg : tregister);virtual;
  124. { that procedure pops a exception value }
  125. procedure g_pop_exception_value_reg(list : paasmoutput;reg : tregister);virtual;
  126. procedure g_return_from_proc(list : paasmoutput;parasize : aword);virtual;
  127. {********************************************************}
  128. { these methods can be overriden for extra functionality }
  129. { the following methods do nothing: }
  130. procedure g_interrupt_stackframe_entry(list : paasmoutput);virtual;
  131. procedure g_interrupt_stackframe_exit(list : paasmoutput);virtual;
  132. procedure g_profilecode(list : paasmoutput);virtual;
  133. procedure g_stackcheck(list : paasmoutput;stackframesize : longint);virtual;
  134. procedure a_load_const_ref(list : paasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  135. procedure g_maybe_loadself(list : paasmoutput);virtual;
  136. { copies len bytes from the source to destination, if }
  137. { loadref is true, it assumes that it first must load }
  138. { the source address from the memory location where }
  139. { source points to }
  140. procedure g_concatcopy(list : paasmoutput;const source,dest : treference;len : aword;loadref : boolean);virtual;
  141. { uses the addr of ref as param, was emitpushreferenceaddr }
  142. procedure a_param_ref_addr(list : paasmoutput;r : treference;nr : longint);virtual;
  143. end;
  144. var
  145. cg : pcg; { this is the main code generator class }
  146. implementation
  147. uses
  148. strings,globals,globtype,options,files,gdb,systems,
  149. ppu,verbose,types,tgobj,tgcpu;
  150. {*****************************************************************************
  151. basic functionallity
  152. ******************************************************************************}
  153. constructor tcg.init;
  154. var
  155. i : longint;
  156. begin
  157. scratch_register_array_pointer:=1;
  158. for i:=1 to max_scratch_regs do
  159. include(unusedscratchregisters,scratch_regs[i]);
  160. end;
  161. destructor tcg.done;
  162. begin
  163. end;
  164. procedure tcg.a_reg_alloc(list : paasmoutput;r : tregister);
  165. begin
  166. list^.concat(new(pairegalloc,alloc(r)));
  167. end;
  168. procedure tcg.a_reg_dealloc(list : paasmoutput;r : tregister);
  169. begin
  170. list^.concat(new(pairegalloc,dealloc(r)));
  171. end;
  172. procedure tcg.a_label(list : paasmoutput;l : pasmlabel);
  173. begin
  174. list^.concat(new(pai_label,init(l)));
  175. end;
  176. function tcg.get_scratch_reg(list : paasmoutput) : tregister;
  177. var
  178. r : tregister;
  179. i : longint;
  180. begin
  181. if unusedscratchregisters=[] then
  182. internalerror(68996);
  183. for i:=scratch_register_array_pointer to
  184. (scratch_register_array_pointer+max_scratch_regs) do
  185. if scratch_regs[(i mod max_scratch_regs)+1] in unusedscratchregisters then
  186. begin
  187. r:=scratch_regs[(i mod max_scratch_regs)+1];
  188. break;
  189. end;
  190. exclude(unusedscratchregisters,r);
  191. inc(scratch_register_array_pointer);
  192. if scratch_register_array_pointer>max_scratch_regs then
  193. scratch_register_array_pointer:=1;
  194. a_reg_alloc(list,r);
  195. get_scratch_reg:=r;
  196. end;
  197. procedure tcg.free_scratch_reg(list : paasmoutput;r : tregister);
  198. begin
  199. include(unusedscratchregisters,r);
  200. a_reg_dealloc(list,r);
  201. end;
  202. {*****************************************************************************
  203. this methods must be overridden for extra functionality
  204. ******************************************************************************}
  205. procedure tcg.g_interrupt_stackframe_entry(list : paasmoutput);
  206. begin
  207. end;
  208. procedure tcg.g_interrupt_stackframe_exit(list : paasmoutput);
  209. begin
  210. end;
  211. procedure tcg.g_profilecode(list : paasmoutput);
  212. begin
  213. end;
  214. {*****************************************************************************
  215. for better code generation these methods should be overridden
  216. ******************************************************************************}
  217. procedure tcg.a_param_const(list : paasmoutput;size : tcgsize;a : aword;nr : longint);
  218. var
  219. hr : tregister;
  220. begin
  221. hr:=get_scratch_reg(list);
  222. a_load_const_reg(list,size,a,hr);
  223. a_param_reg(list,size,hr,nr);
  224. free_scratch_reg(list,hr);
  225. end;
  226. procedure tcg.a_param_ref(list : paasmoutput;size : tcgsize;const r : treference;nr : longint);
  227. var
  228. hr : tregister;
  229. begin
  230. hr:=get_scratch_reg(list);
  231. a_load_ref_reg(list,size,r,hr);
  232. a_param_reg(list,size,hr,nr);
  233. free_scratch_reg(list,hr);
  234. end;
  235. procedure tcg.a_param_ref_addr(list : paasmoutput;r : treference;nr : longint);
  236. var
  237. hr : tregister;
  238. begin
  239. hr:=get_scratch_reg(list);
  240. a_loadaddress_ref_reg(list,r,hr);
  241. a_param_reg(list,OS_ADDR,hr,nr);
  242. free_scratch_reg(list,hr);
  243. end;
  244. procedure tcg.g_stackcheck(list : paasmoutput;stackframesize : longint);
  245. begin
  246. a_param_const(list,OS_32,stackframesize,1);
  247. a_call_name(list,'FPC_STACKCHECK',0);
  248. end;
  249. procedure tcg.a_load_const_ref(list : paasmoutput;size : tcgsize;a : aword;const ref : treference);
  250. var
  251. hr : tregister;
  252. begin
  253. hr:=get_scratch_reg(list);
  254. a_load_const_reg(list,size,a,hr);
  255. a_load_reg_ref(list,size,hr,ref);
  256. free_scratch_reg(list,hr);
  257. end;
  258. procedure tcg.g_concatcopy(list : paasmoutput;const source,dest : treference;len : aword;loadref : boolean);
  259. begin
  260. abstract;
  261. end;
  262. {*****************************************************************************
  263. String helper routines
  264. *****************************************************************************}
  265. procedure tcg.g_removetemps(list : paasmoutput;p : plinkedlist);
  266. var
  267. hp : ptemptodestroy;
  268. pushedregs : tpushed;
  269. begin
  270. hp:=ptemptodestroy(p^.first);
  271. if not(assigned(hp)) then
  272. exit;
  273. tg.pushusedregisters(pushedregs,$ff);
  274. while assigned(hp) do
  275. begin
  276. if is_ansistring(hp^.typ) then
  277. begin
  278. g_decrstrref(list,hp^.address,hp^.typ);
  279. tg.ungetiftemp(hp^.address);
  280. end;
  281. hp:=ptemptodestroy(hp^.next);
  282. end;
  283. tg.popusedregisters(pushedregs);
  284. end;
  285. procedure tcg.g_decrstrref(list : paasmoutput;const ref : treference;t : pdef);
  286. var
  287. pushedregs : tpushed;
  288. begin
  289. tg.pushusedregisters(pushedregs,$ff);
  290. a_param_ref_addr(list,ref,1);
  291. if is_ansistring(t) then
  292. a_call_name(list,'FPC_ANSISTR_DECR_REF',0)
  293. else if is_widestring(t) then
  294. a_call_name(list,'FPC_WIDESTR_DECR_REF',0)
  295. else internalerror(58993);
  296. tg.popusedregisters(pushedregs);
  297. end;
  298. {*****************************************************************************
  299. Code generation for subroutine entry- and exit code
  300. *****************************************************************************}
  301. { initilizes data of type t }
  302. { if is_already_ref is true then the routines assumes }
  303. { that r points to the data to initialize }
  304. procedure tcg.g_initialize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  305. var
  306. hr : treference;
  307. begin
  308. if is_ansistring(t) or
  309. is_widestring(t) then
  310. a_load_const_ref(list,OS_8,0,ref)
  311. else
  312. begin
  313. reset_reference(hr);
  314. hr.symbol:=t^.get_inittable_label;
  315. a_param_ref_addr(list,hr,2);
  316. if is_already_ref then
  317. a_param_ref(list,OS_ADDR,ref,1)
  318. else
  319. a_param_ref_addr(list,ref,1);
  320. a_call_name(list,'FPC_INITIALIZE',0);
  321. end;
  322. end;
  323. procedure tcg.g_finalize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  324. var
  325. r : treference;
  326. begin
  327. if is_ansistring(t) or
  328. is_widestring(t) then
  329. begin
  330. g_decrstrref(list,ref,t);
  331. end
  332. else
  333. begin
  334. reset_reference(r);
  335. r.symbol:=t^.get_inittable_label;
  336. a_param_ref_addr(list,r,2);
  337. if is_already_ref then
  338. a_paramaddr_ref(list,ref,1)
  339. else
  340. a_param_ref_addr(list,ref,1);
  341. a_call_name(list,'FPC_FINALIZE',0);
  342. end;
  343. end;
  344. { generates the code for initialisation of local data }
  345. procedure tcg.g_initialize_data(list : paasmoutput;p : psym);
  346. var
  347. hr : treference;
  348. begin
  349. if (psym(p)^.typ=varsym) and
  350. assigned(pvarsym(p)^.definition) and
  351. not((pvarsym(p)^.definition^.deftype=objectdef) and
  352. pobjectdef(pvarsym(p)^.definition)^.is_class) and
  353. pvarsym(p)^.definition^.needs_inittable then
  354. begin
  355. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  356. reset_reference(hr);
  357. if psym(p)^.owner^.symtabletype=localsymtable then
  358. begin
  359. hr.base:=procinfo^.framepointer;
  360. hr.offset:=-pvarsym(p)^.address;
  361. end
  362. else
  363. begin
  364. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  365. end;
  366. g_initialize(list,pvarsym(p)^.definition,hr,false);
  367. end;
  368. end;
  369. { generates the code for incrementing the reference count of parameters }
  370. procedure tcg.g_incr_data(list : paasmoutput;p : psym);
  371. var
  372. hr : treference;
  373. begin
  374. if (psym(p)^.typ=varsym) and
  375. not((pvarsym(p)^.definition^.deftype=objectdef) and
  376. pobjectdef(pvarsym(p)^.definition)^.is_class) and
  377. pvarsym(p)^.definition^.needs_inittable and
  378. ((pvarsym(p)^.varspez=vs_value)) then
  379. begin
  380. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  381. reset_reference(hr);
  382. hr.symbol:=pvarsym(p)^.definition^.get_inittable_label;
  383. a_param_ref_addr(list,hr,2);
  384. reset_reference(hr);
  385. hr.base:=procinfo^.framepointer;
  386. hr.offset:=pvarsym(p)^.address+procinfo^.call_offset;
  387. a_param_ref_addr(list,hr,1);
  388. reset_reference(hr);
  389. a_call_name(list,'FPC_ADDREF',0);
  390. end;
  391. end;
  392. { generates the code for finalisation of local data }
  393. procedure tcg.g_finalize_data(list : paasmoutput;p : pnamedindexobject);
  394. var
  395. hr : treference;
  396. begin
  397. if (psym(p)^.typ=varsym) and
  398. assigned(pvarsym(p)^.definition) and
  399. not((pvarsym(p)^.definition^.deftype=objectdef) and
  400. pobjectdef(pvarsym(p)^.definition)^.is_class) and
  401. pvarsym(p)^.definition^.needs_inittable then
  402. begin
  403. { not all kind of parameters need to be finalized }
  404. if (psym(p)^.owner^.symtabletype=parasymtable) and
  405. ((pvarsym(p)^.varspez=vs_var) or
  406. (pvarsym(p)^.varspez=vs_const) { and
  407. (dont_copy_const_param(pvarsym(p)^.definition)) } ) then
  408. exit;
  409. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  410. reset_reference(hr);
  411. case psym(p)^.owner^.symtabletype of
  412. localsymtable:
  413. begin
  414. hr.base:=procinfo^.framepointer;
  415. hr.offset:=-pvarsym(p)^.address;
  416. end;
  417. parasymtable:
  418. begin
  419. hr.base:=procinfo^.framepointer;
  420. hr.offset:=pvarsym(p)^.address+procinfo^.call_offset;
  421. end;
  422. else
  423. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  424. end;
  425. g_finalize(list,pvarsym(p)^.definition,hr,false);
  426. end;
  427. end;
  428. { generates the code to make local copies of the value parameters }
  429. procedure tcg.g_copyvalueparas(list : paasmoutput;p : pnamedindexobject);
  430. begin
  431. runerror(255);
  432. end;
  433. var
  434. _list : paasmoutput;
  435. { wrappers for the methods, because TP doesn't know procedures }
  436. { of objects }
  437. procedure _copyvalueparas(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  438. begin
  439. cg^.g_copyvalueparas(_list,s);
  440. end;
  441. procedure tcg.g_finalizetempansistrings(list : paasmoutput);
  442. var
  443. hp : ptemprecord;
  444. hr : treference;
  445. begin
  446. hp:=tg.templist;
  447. while assigned(hp) do
  448. begin
  449. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  450. begin
  451. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  452. reset_reference(hr);
  453. hr.base:=procinfo^.framepointer;
  454. hr.offset:=hp^.pos;
  455. a_param_ref_addr(list,hr,1);
  456. a_call_name(list,'FPC_ANSISTR_DECR_REF',0);
  457. end;
  458. hp:=hp^.next;
  459. end;
  460. end;
  461. procedure _finalize_data(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  462. begin
  463. cg^.g_finalize_data(_list,s);
  464. end;
  465. procedure _incr_data(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  466. begin
  467. cg^.g_incr_data(_list,psym(s));
  468. end;
  469. procedure _initialize_data(s : pnamedindexobject);{$ifndef FPC}far;{$endif}
  470. begin
  471. cg^.g_initialize_data(_list,psym(s));
  472. end;
  473. { generates the entry code for a procedure }
  474. procedure tcg.g_entrycode(list : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  475. stackframe:longint;var parasize:longint;var nostackframe:boolean;
  476. inlined : boolean);
  477. var
  478. hs : string;
  479. hp : pused_unit;
  480. initcode : taasmoutput;
  481. {$ifdef GDB}
  482. stab_function_name : Pai_stab_function_name;
  483. {$endif GDB}
  484. hr : treference;
  485. r : tregister;
  486. begin
  487. { Align }
  488. if (not inlined) then
  489. begin
  490. { gprof uses 16 byte granularity !! }
  491. if (cs_profile in aktmoduleswitches) then
  492. list^.insert(new(pai_align,init(16)))
  493. else
  494. if not(cs_littlesize in aktglobalswitches) then
  495. list^.insert(new(pai_align,init(4)));
  496. end;
  497. { save registers on cdecl }
  498. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  499. begin
  500. for r:=firstreg to lastreg do
  501. begin
  502. if (r in registers_saved_on_cdecl) then
  503. if (r in (tg.availabletempregsint+
  504. tg.availabletempregsfpu+
  505. tg.availabletempregsmm)) then
  506. begin
  507. if not(r in tg.usedinproc) then
  508. {!!!!!!!!!!!! a_push_reg(list,r) }
  509. end
  510. else
  511. {!!!!!!!! a_push_reg(list,r) };
  512. end;
  513. end;
  514. { omit stack frame ? }
  515. if not inlined then
  516. if procinfo^.framepointer=stack_pointer then
  517. begin
  518. CGMessage(cg_d_stackframe_omited);
  519. nostackframe:=true;
  520. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  521. parasize:=0
  522. else
  523. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.call_offset-pointersize;
  524. end
  525. else
  526. begin
  527. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  528. parasize:=0
  529. else
  530. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.call_offset-pointersize*2;
  531. nostackframe:=false;
  532. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  533. g_interrupt_stackframe_entry(list);
  534. g_stackframe_entry(list,stackframe);
  535. if (cs_check_stack in aktlocalswitches) and
  536. (tf_supports_stack_checking in target_info.flags) then
  537. g_stackcheck(@initcode,stackframe);
  538. end;
  539. if cs_profile in aktmoduleswitches then
  540. g_profilecode(@initcode);
  541. if (not inlined) and (aktprocsym^.definition^.proctypeoption in [potype_unitinit]) then
  542. begin
  543. { needs the target a console flags ? }
  544. if tf_needs_isconsole in target_info.flags then
  545. begin
  546. hr.symbol:=newasmsymbol('U_'+target_info.system_unit+'_ISCONSOLE');
  547. if apptype=at_cui then
  548. a_load_const_ref(list,OS_8,1,hr)
  549. else
  550. a_load_const_ref(list,OS_8,0,hr);
  551. dispose(hr.symbol,done);
  552. end;
  553. hp:=pused_unit(usedunits.first);
  554. while assigned(hp) do
  555. begin
  556. { call the unit init code and make it external }
  557. if (hp^.u^.flags and uf_init)<>0 then
  558. a_call_name(list,
  559. 'INIT$$'+hp^.u^.modulename^,0);
  560. hp:=Pused_unit(hp^.next);
  561. end;
  562. end;
  563. {$ifdef dummy}
  564. { a constructor needs a help procedure }
  565. if (aktprocsym^.definition^.options and poconstructor)<>0 then
  566. begin
  567. if procinfo^._class^.isclass then
  568. begin
  569. list^.concat(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol('FPC_NEW_CLASS'))));
  570. list^.concat(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  571. end
  572. else
  573. begin
  574. {
  575. list^.insert(new(pai_labeled,init(A_JZ,quickexitlabel)));
  576. list^.insert(new(paicpu,op_csymbol(A_CALL,S_NO,
  577. newcsymbol('FPC_HELP_CONSTRUCTOR',0))));
  578. list^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
  579. concat_external('FPC_HELP_CONSTRUCTOR',EXT_NEAR);
  580. }
  581. end;
  582. end;
  583. {$endif dummy}
  584. {$ifdef GDB}
  585. if (cs_debuginfo in aktmoduleswitches) then
  586. list^.insert(new(pai_force_line,init));
  587. {$endif GDB}
  588. { initialize return value }
  589. if assigned(procinfo^.retdef) and
  590. is_ansistring(procinfo^.retdef) or
  591. is_widestring(procinfo^.retdef) then
  592. begin
  593. reset_reference(hr);
  594. hr.offset:=procinfo^.retoffset;
  595. hr.base:=procinfo^.framepointer;
  596. a_load_const_ref(list,OS_32,0,hr);
  597. end;
  598. _list:=list;
  599. { generate copies of call by value parameters }
  600. if (po_assembler in aktprocsym^.definition^.procoptions) then
  601. aktprocsym^.definition^.parast^.foreach({$ifdef FPC}@{$endif FPC}_copyvalueparas);
  602. { initialisizes local data }
  603. aktprocsym^.definition^.localst^.foreach({$ifdef FPC}@{$endif FPC}_initialize_data);
  604. { add a reference to all call by value/const parameters }
  605. aktprocsym^.definition^.parast^.foreach({$ifdef FPC}@{$endif FPC}_incr_data);
  606. if (cs_profile in aktmoduleswitches) or
  607. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  608. (assigned(procinfo^._class) and (procinfo^._class^.owner^.symtabletype=globalsymtable)) then
  609. make_global:=true;
  610. if not inlined then
  611. begin
  612. hs:=proc_names.get;
  613. {$ifdef GDB}
  614. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  615. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  616. {$endif GDB}
  617. { insert the names for the procedure }
  618. while hs<>'' do
  619. begin
  620. if make_global then
  621. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  622. else
  623. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  624. {$ifdef GDB}
  625. if (cs_debuginfo in aktmoduleswitches) then
  626. begin
  627. if target_os.use_function_relative_addresses then
  628. list^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  629. end;
  630. {$endif GDB}
  631. hs:=proc_names.get;
  632. end;
  633. end;
  634. {$ifdef GDB}
  635. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  636. begin
  637. if target_os.use_function_relative_addresses then
  638. list^.insert(stab_function_name);
  639. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  640. aktprocsym^.is_global := True;
  641. list^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  642. aktprocsym^.isstabwritten:=true;
  643. end;
  644. {$endif GDB}
  645. end;
  646. procedure tcg.g_exitcode(list : paasmoutput;parasize:longint;nostackframe,inlined:boolean);
  647. var
  648. {$ifdef GDB}
  649. mangled_length : longint;
  650. p : pchar;
  651. {$endif GDB}
  652. nofinal,noreraiselabel : pasmlabel;
  653. hr : treference;
  654. r : tregister;
  655. begin
  656. if aktexitlabel^.is_used then
  657. list^.insert(new(pai_label,init(aktexitlabel)));
  658. { call the destructor help procedure }
  659. if (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  660. begin
  661. if procinfo^._class^.is_class then
  662. a_call_name(list,'FPC_DISPOSE_CLASS',0)
  663. else
  664. begin
  665. if procinfo^._class^.needs_inittable then
  666. begin
  667. getlabel(nofinal);
  668. {!!!!!!!!!!
  669. reset_reference(hr);
  670. hr.base:=R_EBP;
  671. hr.offset:=8;
  672. a_cmp_reg_const_label(list,OS_ADDR,OZ_EQ,
  673. }
  674. reset_reference(hr);
  675. hr.symbol:=procinfo^._class^.get_inittable_label;
  676. a_paramaddr_ref(list,hr,2);
  677. a_param_reg(list,OS_ADDR,self_pointer,1);
  678. a_call_name(list,'FPC_FINALIZE',0);
  679. a_label(list,nofinal);
  680. end;
  681. { vmt_offset_reg can be a scratch register, }
  682. { but it must be always the same }
  683. a_reg_alloc(list,vmt_offset_reg);
  684. a_load_const_reg(list,OS_32,procinfo^._class^.vmt_offset,vmt_offset_reg);
  685. a_call_name(list,'FPC_HELP_DESTRUCTOR',0);
  686. a_reg_dealloc(list,vmt_offset_reg);
  687. end;
  688. end;
  689. { finalize temporary data }
  690. g_finalizetempansistrings(list);
  691. _list:=list;
  692. { finalize local data }
  693. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}_finalize_data);
  694. { finalize paras data }
  695. if assigned(aktprocsym^.definition^.parast) then
  696. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}_finalize_data);
  697. { do we need to handle exceptions because of ansi/widestrings ? }
  698. if (procinfo^.flags and pi_needs_implicit_finally)<>0 then
  699. begin
  700. getlabel(noreraiselabel);
  701. a_call_name(list,'FPC_POPADDRSTACK',0);
  702. a_reg_alloc(list,accumulator);
  703. g_pop_exception_value_reg(list,accumulator);
  704. a_cmp_reg_const_label(list,OS_32,OC_EQ,0,accumulator,noreraiselabel);
  705. a_reg_dealloc(list,accumulator);
  706. { must be the return value finalized before reraising the exception? }
  707. if (procinfo^.retdef<>pdef(voiddef)) and
  708. (procinfo^.retdef^.needs_inittable) and
  709. ((procinfo^.retdef^.deftype<>objectdef) or
  710. not(pobjectdef(procinfo^.retdef)^.is_class)) then
  711. begin
  712. reset_reference(hr);
  713. hr.offset:=procinfo^.retoffset;
  714. hr.base:=procinfo^.framepointer;
  715. g_finalize(list,procinfo^.retdef,hr,ret_in_param(procinfo^.retdef));
  716. end;
  717. a_call_name(list,'FPC_RERAISE',0);
  718. a_label(list,noreraiselabel);
  719. end;
  720. { call __EXIT for main program }
  721. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  722. a_call_name(list,'FPC_DO_EXIT',0);
  723. { handle return value }
  724. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  725. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  726. { handle_return_value(inlined) }
  727. else
  728. begin
  729. { return self in EAX }
  730. a_label(list,quickexitlabel);
  731. a_reg_alloc(list,accumulator);
  732. a_load_reg_reg(list,OS_ADDR,self_pointer,accumulator);
  733. a_reg_dealloc(list,self_pointer);
  734. a_label(list,quickexitlabel);
  735. { we can't clear the zero flag because the Alpha }
  736. { for example doesn't have flags, we have to compare }
  737. { the accu. in the caller }
  738. end;
  739. { stabs uses the label also ! }
  740. if aktexit2label^.is_used or
  741. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  742. a_label(list,aktexit2label);
  743. {$ifdef dummy}
  744. { should we restore edi ? }
  745. { for all i386 gcc implementations }
  746. {!!!!!!!!!!! I don't know how to handle register saving yet }
  747. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  748. begin
  749. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  750. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  751. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  752. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  753. { here we could reset R_EBX
  754. but that is risky because it only works
  755. if genexitcode is called after genentrycode
  756. so lets skip this for the moment PM
  757. aktprocsym^.definition^.usedregisters:=
  758. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  759. }
  760. end;
  761. {$endif dummy}
  762. if not(nostackframe) and not inlined then
  763. g_restore_frame_pointer(list);
  764. { at last, the return is generated }
  765. if not inlined then
  766. if po_interrupt in aktprocsym^.definition^.procoptions then
  767. g_interrupt_stackframe_exit(list)
  768. else
  769. g_return_from_proc(list,parasize);
  770. list^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  771. {$ifdef GDB}
  772. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  773. begin
  774. aktprocsym^.concatstabto(list);
  775. if assigned(procinfo^._class) then
  776. if (not assigned(procinfo^.parent) or
  777. not assigned(procinfo^.parent^._class)) then
  778. list^.concat(new(pai_stabs,init(strpnew(
  779. '"$t:v'+procinfo^._class^.numberstring+'",'+
  780. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset)))));
  781. {!!!!!!!!!!!!
  782. else
  783. list^.concat(new(pai_stabs,init(strpnew(
  784. '"$t:r'+procinfo^._class^.numberstring+'",'+
  785. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  786. }
  787. if (pdef(aktprocsym^.definition^.retdef) <> pdef(voiddef)) then
  788. begin
  789. if ret_in_param(aktprocsym^.definition^.retdef) then
  790. list^.concat(new(pai_stabs,init(strpnew(
  791. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  792. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))))
  793. else
  794. list^.concat(new(pai_stabs,init(strpnew(
  795. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  796. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))));
  797. if (m_result in aktmodeswitches) then
  798. if ret_in_param(aktprocsym^.definition^.retdef) then
  799. list^.concat(new(pai_stabs,init(strpnew(
  800. '"RESULT:X*'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  801. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))))
  802. else
  803. list^.concat(new(pai_stabs,init(strpnew(
  804. '"RESULT:X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  805. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))));
  806. end;
  807. mangled_length:=length(aktprocsym^.definition^.mangledname);
  808. getmem(p,mangled_length+50);
  809. strpcopy(p,'192,0,0,');
  810. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  811. list^.concat(new(pai_stabn,init(strnew(p))));
  812. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  813. +aktprocsym^.definition^.mangledname))));
  814. p[0]:='2';p[1]:='2';p[2]:='4';
  815. strpcopy(strend(p),'_end');}
  816. freemem(p,mangled_length+50);
  817. list^.concat(new(pai_stabn,init(
  818. strpnew('224,0,0,'+aktexit2label^.name))));
  819. { strpnew('224,0,0,'
  820. +aktprocsym^.definition^.mangledname+'_end'))));}
  821. end;
  822. {$endif GDB}
  823. end;
  824. {*****************************************************************************
  825. some abstract definitions
  826. ****************************************************************************}
  827. procedure tcg.a_call_name(list : paasmoutput;const s : string;
  828. offset : longint);
  829. begin
  830. abstract;
  831. end;
  832. procedure tcg.g_stackframe_entry(list : paasmoutput;localsize : longint);
  833. begin
  834. abstract;
  835. end;
  836. procedure tcg.g_maybe_loadself(list : paasmoutput);
  837. begin
  838. abstract;
  839. end;
  840. procedure tcg.g_restore_frame_pointer(list : paasmoutput);
  841. begin
  842. abstract;
  843. end;
  844. procedure g_return_from_proc(list : paasmoutput;parasize : aword);
  845. begin
  846. abstract;
  847. end;
  848. procedure tcg.a_loadaddress_ref_reg(list : paasmoutput;const ref : treference;r : tregister);
  849. begin
  850. abstract;
  851. end;
  852. procedure tcg.g_push_exception_value_reg(list : paasmoutput;reg : tregister);
  853. begin
  854. abstract;
  855. end;
  856. procedure tcg.g_push_exception_value_const(list : paasmoutput;reg : tregister);
  857. begin
  858. abstract;
  859. end;
  860. procedure tcg.g_pop_exception_value_reg(list : paasmoutput;reg : tregister);
  861. begin
  862. abstract;
  863. end;
  864. procedure tcg.a_load_const_reg(list : paasmoutput;size : tcgsize;a : aword;register : tregister);
  865. begin
  866. abstract;
  867. end;
  868. procedure tcg.a_load_reg_ref(list : paasmoutput;size : tcgsize;register : tregister;const ref : treference);
  869. begin
  870. abstract;
  871. end;
  872. procedure tcg.a_load_ref_reg(list : paasmoutput;size : tcgsize;const ref : treference;register : tregister);
  873. begin
  874. abstract;
  875. end;
  876. procedure tcg.a_load_reg_reg(list : paasmoutput;size : tcgsize;reg1,reg2 : tregister);
  877. begin
  878. abstract;
  879. end;
  880. procedure tcg.a_cmp_reg_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  881. l : pasmlabel);
  882. begin
  883. abstract;
  884. end;
  885. procedure tcg.a_cmp_reg_reg_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : pasmlabel);
  886. begin
  887. abstract;
  888. end;
  889. procedure tcg.a_cmp_reg_ref_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister;l : pasmlabel);
  890. begin
  891. abstract;
  892. end;
  893. procedure tcg.a_cmp_ref_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  894. l : pasmlabel);
  895. begin
  896. abstract;
  897. end;
  898. procedure tcg.a_jmp_cond(list : paasmoutput;cond : TOpCmp;l: pasmlabel);
  899. begin
  900. abstract;
  901. end;
  902. procedure tcg.g_return_from_proc(list : paasmoutput;parasize : aword);
  903. begin
  904. abstract;
  905. end;
  906. procedure tcg.a_param_reg(list : paasmoutput;size : tcgsize;r : tregister;nr : longint);
  907. begin
  908. abstract;
  909. end;
  910. procedure tcg.a_paramaddr_ref(list : paasmoutput;const r : treference;nr : longint);
  911. begin
  912. abstract;
  913. end;
  914. end.
  915. {
  916. $Log$
  917. Revision 1.30 1999-11-05 07:05:56 jonas
  918. + a_jmp_cond()
  919. Revision 1.29 1999/10/21 16:41:41 florian
  920. * problems with readln fixed: esi wasn't restored correctly when
  921. reading ordinal fields of objects futher the register allocation
  922. didn't take care of the extra register when reading ordinal values
  923. * enumerations can now be used in constant indexes of properties
  924. Revision 1.28 1999/10/12 21:20:46 florian
  925. * new codegenerator compiles again
  926. Revision 1.27 1999/09/29 11:46:20 florian
  927. * fixed bug 292 from bugs directory
  928. Revision 1.26 1999/09/14 11:16:09 florian
  929. * only small updates to work with the current compiler
  930. Revision 1.25 1999/09/03 13:09:09 jonas
  931. * fixed typo regarding scratchregs pointer
  932. Revision 1.24 1999/08/26 14:51:54 jonas
  933. * changed get_scratch_reg so it actually uses the
  934. scratch_reg_array_pointer
  935. Revision 1.23 1999/08/25 12:00:11 jonas
  936. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  937. Revision 1.22 1999/08/18 17:05:55 florian
  938. + implemented initilizing of data for the new code generator
  939. so it should compile now simple programs
  940. Revision 1.21 1999/08/07 14:21:08 florian
  941. * some small problems fixed
  942. Revision 1.20 1999/08/06 18:05:52 florian
  943. * implemented some stuff for assignments
  944. Revision 1.19 1999/08/06 17:00:54 florian
  945. + definition of concatcopy
  946. Revision 1.18 1999/08/06 16:37:45 jonas
  947. * completed bugfix done by Florian o I wouldn't get conflicts :)
  948. Revision 1.17 1999/08/06 16:27:26 florian
  949. * for Jonas: else he will get conflicts
  950. Revision 1.16 1999/08/06 16:04:05 michael
  951. + introduced tainstruction
  952. Revision 1.15 1999/08/06 15:53:50 florian
  953. * made the alpha version compilable
  954. Revision 1.14 1999/08/06 14:15:51 florian
  955. * made the alpha version compilable
  956. Revision 1.13 1999/08/06 13:26:50 florian
  957. * more changes ...
  958. Revision 1.12 1999/08/05 17:10:56 florian
  959. * some more additions, especially procedure
  960. exit code generation
  961. Revision 1.11 1999/08/05 14:58:11 florian
  962. * some fixes for the floating point registers
  963. * more things for the new code generator
  964. Revision 1.10 1999/08/04 00:23:52 florian
  965. * renamed i386asm and i386base to cpuasm and cpubase
  966. Revision 1.9 1999/08/02 23:13:21 florian
  967. * more changes to compile for the Alpha
  968. Revision 1.8 1999/08/02 17:14:07 florian
  969. + changed the temp. generator to an object
  970. Revision 1.7 1999/08/01 23:05:55 florian
  971. * changes to compile with FPC
  972. Revision 1.6 1999/08/01 18:22:33 florian
  973. * made it again compilable
  974. Revision 1.5 1999/01/23 23:29:46 florian
  975. * first running version of the new code generator
  976. * when compiling exceptions under Linux fixed
  977. Revision 1.4 1999/01/13 22:52:36 florian
  978. + YES, finally the new code generator is compilable, but it doesn't run yet :(
  979. Revision 1.3 1998/12/26 15:20:30 florian
  980. + more changes for the new version
  981. Revision 1.2 1998/12/15 22:18:55 florian
  982. * some code added
  983. Revision 1.1 1998/12/15 16:32:58 florian
  984. + first version, derived from old routines
  985. }