hcodegen.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. This unit exports some help routines for the code generation
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit hcodegen;
  19. interface
  20. uses
  21. aasm,tree,symtable
  22. {$ifdef i386}
  23. ,i386
  24. {$endif}
  25. {$ifdef m68k}
  26. ,m68k
  27. {$endif}
  28. ;
  29. const
  30. pi_uses_asm = $1; { set, if the procedure uses asm }
  31. pi_is_global = $2; { set, if the procedure is exported by an unit }
  32. pi_do_call = $4; { set, if the procedure does a call }
  33. pi_operator = $8; { set, if the procedure is an operator }
  34. pi_C_import = $10; { set, if the procedure is an external C function }
  35. pi_uses_exceptions = $20;{ set, if the procedure has a try statement => }
  36. { no register variables }
  37. type
  38. pprocinfo = ^tprocinfo;
  39. tprocinfo = record
  40. { pointer to parent in nested procedures }
  41. parent : pprocinfo;
  42. { current class, if we are in a method }
  43. _class : pobjectdef;
  44. { return type }
  45. retdef : pdef;
  46. { return type }
  47. sym : pprocsym;
  48. { symbol of the function }
  49. funcretsym : pfuncretsym;
  50. { the definition of the proc itself }
  51. { why was this a pdef only ?? PM }
  52. def : pprocdef;
  53. { frame pointer offset }
  54. framepointer_offset : longint;
  55. { self pointer offset }
  56. ESI_offset : longint;
  57. { result value offset }
  58. retoffset : longint;
  59. { firsttemp position }
  60. firsttemp : longint;
  61. funcret_is_valid : boolean;
  62. { parameter offset }
  63. call_offset : longint;
  64. { some collected informations about the procedure }
  65. { see pi_xxxx above }
  66. flags : longint;
  67. { register used as frame pointer }
  68. framepointer : tregister;
  69. { true, if the procedure is exported by an unit }
  70. globalsymbol : boolean;
  71. { true, if the procedure should be exported (only OS/2) }
  72. exported : boolean;
  73. { code for the current procedure }
  74. aktproccode,aktentrycode,
  75. aktexitcode,aktlocaldata : paasmoutput;
  76. { local data is used for smartlink }
  77. end;
  78. var
  79. { info about the current sub routine }
  80. procinfo : tprocinfo;
  81. { labels for BREAK and CONTINUE }
  82. aktbreaklabel,aktcontinuelabel : plabel;
  83. { label when the result is true or false }
  84. truelabel,falselabel : plabel;
  85. { label to leave the sub routine }
  86. aktexitlabel : plabel;
  87. { also an exit label, only used we need to clear only the stack }
  88. aktexit2label : plabel;
  89. { only used in constructor for fail or if getmem fails }
  90. quickexitlabel : plabel;
  91. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  92. simple_loadn : boolean;
  93. { tries to hold the amount of times which the current tree is processed }
  94. t_times : longint;
  95. { true, if an error while code generation occurs }
  96. codegenerror : boolean;
  97. { initialize respectively terminates the code generator }
  98. { for a new module or procedure }
  99. procedure codegen_doneprocedure;
  100. procedure codegen_donemodule;
  101. procedure codegen_newmodule;
  102. procedure codegen_newprocedure;
  103. { counts the labels }
  104. function case_count_labels(root : pcaserecord) : longint;
  105. { searches the highest label }
  106. function case_get_max(root : pcaserecord) : longint;
  107. { searches the lowest label }
  108. function case_get_min(root : pcaserecord) : longint;
  109. { concates/inserts the ASCII string to the data segment }
  110. procedure generate_ascii(const hs : string);
  111. procedure generate_ascii_insert(const hs : string);
  112. { concates/inserts the ASCII string from pchar to the data segment }
  113. { WARNING : if hs has no #0 and strlen(hs)=length }
  114. { the terminal zero is not written }
  115. procedure generate_pascii(a : paasmoutput;hs : pchar;length : longint);
  116. procedure generate_pascii_insert(hs : pchar;length : longint);
  117. { convert/concats a label for constants in the consts section }
  118. function constlabel2str(l : plabel;ctype:tconsttype):string;
  119. function constlabelnb2str(pnb : longint;ctype:tconsttype):string;
  120. procedure concat_constlabel(p:plabel;ctype:tconsttype);
  121. { to be able to force to have a global label for const }
  122. const
  123. make_const_global : boolean = false;
  124. implementation
  125. uses
  126. systems,cobjects,verbose,globals,files,strings;
  127. {*****************************************************************************
  128. initialize/terminate the codegen for procedure and modules
  129. *****************************************************************************}
  130. procedure codegen_newprocedure;
  131. begin
  132. aktbreaklabel:=nil;
  133. aktcontinuelabel:=nil;
  134. { aktexitlabel:=0; is store in oldaktexitlabel
  135. so it must not be reset to zero before this storage !}
  136. { the type of this lists isn't important }
  137. { because the code of this lists is }
  138. { copied to the code segment }
  139. procinfo.aktentrycode:=new(paasmoutput,init);
  140. procinfo.aktexitcode:=new(paasmoutput,init);
  141. procinfo.aktproccode:=new(paasmoutput,init);
  142. procinfo.aktlocaldata:=new(paasmoutput,init);
  143. end;
  144. procedure codegen_doneprocedure;
  145. begin
  146. dispose(procinfo.aktentrycode,done);
  147. dispose(procinfo.aktexitcode,done);
  148. dispose(procinfo.aktproccode,done);
  149. dispose(procinfo.aktlocaldata,done);
  150. end;
  151. procedure codegen_newmodule;
  152. begin
  153. exprasmlist:=new(paasmoutput,init);
  154. datasegment:=new(paasmoutput,init);
  155. codesegment:=new(paasmoutput,init);
  156. bsssegment:=new(paasmoutput,init);
  157. debuglist:=new(paasmoutput,init);
  158. externals:=new(paasmoutput,init);
  159. internals:=new(paasmoutput,init);
  160. consts:=new(paasmoutput,init);
  161. rttilist:=new(paasmoutput,init);
  162. importssection:=nil;
  163. exportssection:=nil;
  164. resourcesection:=nil;
  165. end;
  166. procedure codegen_donemodule;
  167. begin
  168. dispose(exprasmlist,done);
  169. dispose(codesegment,done);
  170. dispose(bsssegment,done);
  171. dispose(datasegment,done);
  172. dispose(debuglist,done);
  173. dispose(externals,done);
  174. dispose(consts,done);
  175. dispose(rttilist,done);
  176. if assigned(importssection) then
  177. dispose(importssection,done);
  178. if assigned(exportssection) then
  179. dispose(exportssection,done);
  180. if assigned(resourcesection) then
  181. dispose(resourcesection,done);
  182. end;
  183. {*****************************************************************************
  184. Case Helpers
  185. *****************************************************************************}
  186. function case_count_labels(root : pcaserecord) : longint;
  187. var
  188. _l : longint;
  189. procedure count(p : pcaserecord);
  190. begin
  191. inc(_l);
  192. if assigned(p^.less) then
  193. count(p^.less);
  194. if assigned(p^.greater) then
  195. count(p^.greater);
  196. end;
  197. begin
  198. _l:=0;
  199. count(root);
  200. case_count_labels:=_l;
  201. end;
  202. function case_get_max(root : pcaserecord) : longint;
  203. var
  204. hp : pcaserecord;
  205. begin
  206. hp:=root;
  207. while assigned(hp^.greater) do
  208. hp:=hp^.greater;
  209. case_get_max:=hp^._high;
  210. end;
  211. function case_get_min(root : pcaserecord) : longint;
  212. var
  213. hp : pcaserecord;
  214. begin
  215. hp:=root;
  216. while assigned(hp^.less) do
  217. hp:=hp^.less;
  218. case_get_min:=hp^._low;
  219. end;
  220. {*****************************************************************************
  221. String Helpers
  222. *****************************************************************************}
  223. procedure generate_ascii(const hs : string);
  224. begin
  225. datasegment^.concat(new(pai_string,init(hs)))
  226. end;
  227. procedure generate_ascii_insert(const hs : string);
  228. begin
  229. datasegment^.insert(new(pai_string,init(hs)));
  230. end;
  231. function strnew(p : pchar;length : longint) : pchar;
  232. var
  233. pc : pchar;
  234. begin
  235. getmem(pc,length);
  236. move(p^,pc^,length);
  237. strnew:=pc;
  238. end;
  239. { concates the ASCII string from pchar to the asmslist a }
  240. procedure generate_pascii(a : paasmoutput;hs : pchar;length : longint);
  241. var
  242. real_end,current_begin,current_end : pchar;
  243. c :char;
  244. begin
  245. if assigned(hs) then
  246. begin
  247. current_begin:=hs;
  248. real_end:=strend(hs);
  249. c:=hs[0];
  250. while length>32 do
  251. begin
  252. { restore the char displaced }
  253. current_begin[0]:=c;
  254. current_end:=current_begin+32;
  255. { store the char for next loop }
  256. c:=current_end[0];
  257. current_end[0]:=#0;
  258. a^.concat(new(pai_string,init_length_pchar(strnew(current_begin,32),32)));
  259. current_begin:=current_end;
  260. length:=length-32;
  261. end;
  262. current_begin[0]:=c;
  263. a^.concat(new(pai_string,init_length_pchar(strnew(current_begin,length),length)));
  264. end;
  265. end;
  266. { inserts the ASCII string from pchar to the const segment }
  267. procedure generate_pascii_insert(hs : pchar;length : longint);
  268. var
  269. real_end,current_begin,current_end : pchar;
  270. c :char;
  271. begin
  272. if assigned(hs) then
  273. begin
  274. current_begin:=hs;
  275. real_end:=strend(hs);
  276. c:=hs[0];
  277. length:=longint(real_end)-longint(hs);
  278. while length>32 do
  279. begin
  280. { restore the char displaced }
  281. current_begin[0]:=c;
  282. current_end:=current_begin+32;
  283. { store the char for next loop }
  284. c:=current_end[0];
  285. current_end[0]:=#0;
  286. datasegment^.insert(new(pai_string,init_length_pchar(strnew(current_begin,32),32)));
  287. length:=length-32;
  288. end;
  289. datasegment^.insert(new(pai_string,init_length_pchar(strnew(current_begin,length),length)));
  290. end;
  291. end;
  292. {*****************************************************************************
  293. Const Helpers
  294. *****************************************************************************}
  295. const
  296. consttypestr : array[tconsttype] of string[6]=
  297. ('ord','string','real','bool','int','char','set');
  298. { Peter this gives problems for my inlines !! }
  299. { we must use the number directly !!! (PM) }
  300. function constlabel2str(l : plabel;ctype:tconsttype):string;
  301. begin
  302. if (cs_smartlink in aktmoduleswitches) or
  303. make_const_global {or (aktoutputformat in [as_tasm])} then
  304. constlabel2str:='_$'+current_module^.modulename^+'$'+consttypestr[ctype]+'_const_'+tostr(l^.nb)
  305. else
  306. constlabel2str:=lab2str(l);
  307. end;
  308. function constlabelnb2str(pnb : longint;ctype:tconsttype):string;
  309. begin
  310. if (cs_smartlink in aktmoduleswitches) or
  311. make_const_global {or (aktoutputformat in [as_tasm])} then
  312. constlabelnb2str:='_$'+current_module^.modulename^+'$'+consttypestr[ctype]+'_const_'+tostr(pnb)
  313. else
  314. constlabelnb2str:=target_asm.labelprefix+tostr(pnb);
  315. end;
  316. procedure concat_constlabel(p:plabel;ctype:tconsttype);
  317. var
  318. s : string;
  319. begin
  320. if (cs_smartlink in aktmoduleswitches) or
  321. make_const_global {or (aktoutputformat in [as_tasm])} then
  322. begin
  323. s:='_$'+current_module^.modulename^+'$'+consttypestr[ctype]+'_const_'+tostr(p^.nb);
  324. if (cs_smartlink in aktmoduleswitches) then
  325. begin
  326. consts^.concat(new(pai_cut,init));
  327. consts^.concat(new(pai_symbol,init_global(s)))
  328. end
  329. else
  330. consts^.concat(new(pai_symbol,init_global(s)));
  331. end
  332. else
  333. consts^.concat(new(pai_label,init(p)));
  334. end;
  335. end.
  336. {
  337. $Log$
  338. Revision 1.14 1998-08-21 14:08:43 pierre
  339. + TEST_FUNCRET now default (old code removed)
  340. works also for m68k (at least compiles)
  341. Revision 1.13 1998/08/20 09:26:38 pierre
  342. + funcret setting in underproc testing
  343. compile with _dTEST_FUNCRET
  344. Revision 1.12 1998/08/10 14:50:01 peter
  345. + localswitches, moduleswitches, globalswitches splitting
  346. Revision 1.11 1998/07/28 21:52:51 florian
  347. + implementation of raise and try..finally
  348. + some misc. exception stuff
  349. Revision 1.10 1998/07/20 18:40:13 florian
  350. * handling of ansi string constants should now work
  351. Revision 1.9 1998/06/05 16:13:34 pierre
  352. * fix for real and string consts inside inlined procs
  353. Revision 1.8 1998/06/04 23:51:40 peter
  354. * m68k compiles
  355. + .def file creation moved to gendef.pas so it could also be used
  356. for win32
  357. Revision 1.7 1998/06/04 09:55:38 pierre
  358. * demangled name of procsym reworked to become independant of the mangling scheme
  359. Revision 1.6 1998/05/23 01:21:08 peter
  360. + aktasmmode, aktoptprocessor, aktoutputformat
  361. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  362. + $LIBNAME to set the library name where the unit will be put in
  363. * splitted cgi386 a bit (codeseg to large for bp7)
  364. * nasm, tasm works again. nasm moved to ag386nsm.pas
  365. Revision 1.5 1998/05/20 09:42:34 pierre
  366. + UseTokenInfo now default
  367. * unit in interface uses and implementation uses gives error now
  368. * only one error for unknown symbol (uses lastsymknown boolean)
  369. the problem came from the label code !
  370. + first inlined procedures and function work
  371. (warning there might be allowed cases were the result is still wrong !!)
  372. * UseBrower updated gives a global list of all position of all used symbols
  373. with switch -gb
  374. Revision 1.4 1998/05/07 00:17:01 peter
  375. * smartlinking for sets
  376. + consts labels are now concated/generated in hcodegen
  377. * moved some cpu code to cga and some none cpu depended code from cga
  378. to tree and hcodegen and cleanup of hcodegen
  379. * assembling .. output reduced for smartlinking ;)
  380. Revision 1.3 1998/05/06 08:38:40 pierre
  381. * better position info with UseTokenInfo
  382. UseTokenInfo greatly simplified
  383. + added check for changed tree after first time firstpass
  384. (if we could remove all the cases were it happen
  385. we could skip all firstpass if firstpasscount > 1)
  386. Only with ExtDebug
  387. Revision 1.2 1998/04/29 10:33:53 pierre
  388. + added some code for ansistring (not complete nor working yet)
  389. * corrected operator overloading
  390. * corrected nasm output
  391. + started inline procedures
  392. + added starstarn : use ** for exponentiation (^ gave problems)
  393. + started UseTokenInfo cond to get accurate positions
  394. }