hcodegen.pas 15 KB

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