hcodegen.pas 15 KB

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