hcodegen.pas 16 KB

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