hcodegen.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. verbose,aasm,tree,symtable,cobjects
  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. { some kind of temp. types needs to be destructed }
  79. { for example ansistring, this is done using this }
  80. { list }
  81. ttemptodestroy = object(tlinkedlist_item)
  82. typ : tdef;
  83. address : treference;
  84. constructor init(const a : treference;t : tdef);
  85. end;
  86. var
  87. { info about the current sub routine }
  88. procinfo : tprocinfo;
  89. { labels for BREAK and CONTINUE }
  90. aktbreaklabel,aktcontinuelabel : plabel;
  91. { label when the result is true or false }
  92. truelabel,falselabel : plabel;
  93. { label to leave the sub routine }
  94. aktexitlabel : plabel;
  95. { also an exit label, only used we need to clear only the stack }
  96. aktexit2label : plabel;
  97. { only used in constructor for fail or if getmem fails }
  98. quickexitlabel : plabel;
  99. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  100. simple_loadn : boolean;
  101. { tries to hold the amount of times which the current tree is processed }
  102. t_times : longint;
  103. { true, if an error while code generation occurs }
  104. codegenerror : boolean;
  105. { message calls with codegenerror support }
  106. procedure cgmessage(const t : tmsgconst);
  107. procedure cgmessage1(const t : tmsgconst;const s : string);
  108. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  109. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  110. { initialize respectively terminates the code generator }
  111. { for a new module or procedure }
  112. procedure codegen_doneprocedure;
  113. procedure codegen_donemodule;
  114. procedure codegen_newmodule;
  115. procedure codegen_newprocedure;
  116. { counts the labels }
  117. function case_count_labels(root : pcaserecord) : longint;
  118. { searches the highest label }
  119. function case_get_max(root : pcaserecord) : longint;
  120. { searches the lowest label }
  121. function case_get_min(root : pcaserecord) : longint;
  122. { concates/inserts the ASCII string to the data segment }
  123. procedure generate_ascii(a : paasmoutput;const hs : string);
  124. { concates/inserts the ASCII string from pchar to the data segment }
  125. { WARNING : if hs has no #0 and strlen(hs)=length }
  126. { the terminal zero is not written }
  127. procedure generate_pascii(a : paasmoutput;hs : pchar;length : longint);
  128. { convert/concats a label for constants in the consts section }
  129. { function constlabel2str(l : plabel;ctype:tconsttype):string;
  130. function constlabelnb2str(pnb : longint;ctype:tconsttype):string;
  131. procedure concat_constlabel(p:plabel;ctype:tconsttype); }
  132. { to be able to force to have a global label for const }
  133. const
  134. make_const_global : boolean = false;
  135. var
  136. temptoremove : tlinkedlist;
  137. implementation
  138. uses
  139. systems,comphook,globals,files,strings;
  140. {*****************************************************************************
  141. override the message calls to set codegenerror
  142. *****************************************************************************}
  143. procedure cgmessage(const t : tmsgconst);
  144. var
  145. olderrorcount : longint;
  146. begin
  147. if not(codegenerror) then
  148. begin
  149. olderrorcount:=status.errorcount;
  150. verbose.Message(t);
  151. codegenerror:=olderrorcount<>status.errorcount;
  152. end;
  153. end;
  154. procedure cgmessage1(const t : tmsgconst;const s : string);
  155. var
  156. olderrorcount : longint;
  157. begin
  158. if not(codegenerror) then
  159. begin
  160. olderrorcount:=status.errorcount;
  161. verbose.Message1(t,s);
  162. codegenerror:=olderrorcount<>status.errorcount;
  163. end;
  164. end;
  165. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  166. var
  167. olderrorcount : longint;
  168. begin
  169. if not(codegenerror) then
  170. begin
  171. olderrorcount:=status.errorcount;
  172. verbose.Message2(t,s1,s2);
  173. codegenerror:=olderrorcount<>status.errorcount;
  174. end;
  175. end;
  176. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  177. var
  178. olderrorcount : longint;
  179. begin
  180. if not(codegenerror) then
  181. begin
  182. olderrorcount:=status.errorcount;
  183. verbose.Message3(t,s1,s2,s3);
  184. codegenerror:=olderrorcount<>status.errorcount;
  185. end;
  186. end;
  187. {*****************************************************************************
  188. initialize/terminate the codegen for procedure and modules
  189. *****************************************************************************}
  190. procedure codegen_newprocedure;
  191. begin
  192. aktbreaklabel:=nil;
  193. aktcontinuelabel:=nil;
  194. { aktexitlabel:=0; is store in oldaktexitlabel
  195. so it must not be reset to zero before this storage !}
  196. { the type of this lists isn't important }
  197. { because the code of this lists is }
  198. { copied to the code segment }
  199. procinfo.aktentrycode:=new(paasmoutput,init);
  200. procinfo.aktexitcode:=new(paasmoutput,init);
  201. procinfo.aktproccode:=new(paasmoutput,init);
  202. procinfo.aktlocaldata:=new(paasmoutput,init);
  203. end;
  204. procedure codegen_doneprocedure;
  205. begin
  206. dispose(procinfo.aktentrycode,done);
  207. dispose(procinfo.aktexitcode,done);
  208. dispose(procinfo.aktproccode,done);
  209. dispose(procinfo.aktlocaldata,done);
  210. end;
  211. procedure codegen_newmodule;
  212. begin
  213. exprasmlist:=new(paasmoutput,init);
  214. datasegment:=new(paasmoutput,init);
  215. codesegment:=new(paasmoutput,init);
  216. bsssegment:=new(paasmoutput,init);
  217. debuglist:=new(paasmoutput,init);
  218. externals:=new(paasmoutput,init);
  219. internals:=new(paasmoutput,init);
  220. consts:=new(paasmoutput,init);
  221. rttilist:=new(paasmoutput,init);
  222. importssection:=nil;
  223. exportssection:=nil;
  224. resourcesection:=nil;
  225. end;
  226. procedure codegen_donemodule;
  227. begin
  228. dispose(exprasmlist,done);
  229. dispose(codesegment,done);
  230. dispose(bsssegment,done);
  231. dispose(datasegment,done);
  232. dispose(debuglist,done);
  233. dispose(externals,done);
  234. dispose(internals,done);
  235. dispose(consts,done);
  236. dispose(rttilist,done);
  237. if assigned(importssection) then
  238. dispose(importssection,done);
  239. if assigned(exportssection) then
  240. dispose(exportssection,done);
  241. if assigned(resourcesection) then
  242. dispose(resourcesection,done);
  243. end;
  244. {*****************************************************************************
  245. Case Helpers
  246. *****************************************************************************}
  247. function case_count_labels(root : pcaserecord) : longint;
  248. var
  249. _l : longint;
  250. procedure count(p : pcaserecord);
  251. begin
  252. inc(_l);
  253. if assigned(p^.less) then
  254. count(p^.less);
  255. if assigned(p^.greater) then
  256. count(p^.greater);
  257. end;
  258. begin
  259. _l:=0;
  260. count(root);
  261. case_count_labels:=_l;
  262. end;
  263. function case_get_max(root : pcaserecord) : longint;
  264. var
  265. hp : pcaserecord;
  266. begin
  267. hp:=root;
  268. while assigned(hp^.greater) do
  269. hp:=hp^.greater;
  270. case_get_max:=hp^._high;
  271. end;
  272. function case_get_min(root : pcaserecord) : longint;
  273. var
  274. hp : pcaserecord;
  275. begin
  276. hp:=root;
  277. while assigned(hp^.less) do
  278. hp:=hp^.less;
  279. case_get_min:=hp^._low;
  280. end;
  281. {*****************************************************************************
  282. String Helpers
  283. *****************************************************************************}
  284. procedure generate_ascii(a : paasmoutput;const hs : string);
  285. begin
  286. a^.concat(new(pai_string,init(hs)))
  287. end;
  288. function strnew(p : pchar;length : longint) : pchar;
  289. var
  290. pc : pchar;
  291. begin
  292. getmem(pc,length);
  293. move(p^,pc^,length);
  294. strnew:=pc;
  295. end;
  296. { concates the ASCII string from pchar to the asmslist a }
  297. procedure generate_pascii(a : paasmoutput;hs : pchar;length : longint);
  298. var
  299. real_end,current_begin,current_end : pchar;
  300. c :char;
  301. begin
  302. if assigned(hs) then
  303. begin
  304. current_begin:=hs;
  305. real_end:=strend(hs);
  306. c:=hs[0];
  307. while length>32 do
  308. begin
  309. { restore the char displaced }
  310. current_begin[0]:=c;
  311. current_end:=current_begin+32;
  312. { store the char for next loop }
  313. c:=current_end[0];
  314. current_end[0]:=#0;
  315. a^.concat(new(pai_string,init_length_pchar(strnew(current_begin,32),32)));
  316. current_begin:=current_end;
  317. length:=length-32;
  318. end;
  319. current_begin[0]:=c;
  320. a^.concat(new(pai_string,init_length_pchar(strnew(current_begin,length),length)));
  321. end;
  322. end;
  323. constructor ttemptodestroy.init(const a : treference;t : tdef);
  324. begin
  325. inherited init;
  326. address:=a;
  327. typ:=t;
  328. end;
  329. end.
  330. {
  331. $Log$
  332. Revision 1.19 1998-10-26 22:58:18 florian
  333. * new introduded problem with classes fix, the parent class wasn't set
  334. correct, if the class was defined forward before
  335. Revision 1.18 1998/10/06 17:16:50 pierre
  336. * some memory leaks fixed (thanks to Peter for heaptrc !)
  337. Revision 1.17 1998/09/17 09:42:37 peter
  338. + pass_2 for cg386
  339. * Message() -> CGMessage() for pass_1/pass_2
  340. Revision 1.16 1998/09/07 18:46:04 peter
  341. * update smartlinking, uses getdatalabel
  342. * renamed ptree.value vars to value_str,value_real,value_set
  343. Revision 1.15 1998/09/01 09:02:51 peter
  344. * moved message() to hcodegen, so pass_2 also uses them
  345. Revision 1.14 1998/08/21 14:08:43 pierre
  346. + TEST_FUNCRET now default (old code removed)
  347. works also for m68k (at least compiles)
  348. Revision 1.13 1998/08/20 09:26:38 pierre
  349. + funcret setting in underproc testing
  350. compile with _dTEST_FUNCRET
  351. Revision 1.12 1998/08/10 14:50:01 peter
  352. + localswitches, moduleswitches, globalswitches splitting
  353. Revision 1.11 1998/07/28 21:52:51 florian
  354. + implementation of raise and try..finally
  355. + some misc. exception stuff
  356. Revision 1.10 1998/07/20 18:40:13 florian
  357. * handling of ansi string constants should now work
  358. Revision 1.9 1998/06/05 16:13:34 pierre
  359. * fix for real and string consts inside inlined procs
  360. Revision 1.8 1998/06/04 23:51:40 peter
  361. * m68k compiles
  362. + .def file creation moved to gendef.pas so it could also be used
  363. for win32
  364. Revision 1.7 1998/06/04 09:55:38 pierre
  365. * demangled name of procsym reworked to become independant of the mangling scheme
  366. Revision 1.6 1998/05/23 01:21:08 peter
  367. + aktasmmode, aktoptprocessor, aktoutputformat
  368. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  369. + $LIBNAME to set the library name where the unit will be put in
  370. * splitted cgi386 a bit (codeseg to large for bp7)
  371. * nasm, tasm works again. nasm moved to ag386nsm.pas
  372. Revision 1.5 1998/05/20 09:42:34 pierre
  373. + UseTokenInfo now default
  374. * unit in interface uses and implementation uses gives error now
  375. * only one error for unknown symbol (uses lastsymknown boolean)
  376. the problem came from the label code !
  377. + first inlined procedures and function work
  378. (warning there might be allowed cases were the result is still wrong !!)
  379. * UseBrower updated gives a global list of all position of all used symbols
  380. with switch -gb
  381. Revision 1.4 1998/05/07 00:17:01 peter
  382. * smartlinking for sets
  383. + consts labels are now concated/generated in hcodegen
  384. * moved some cpu code to cga and some none cpu depended code from cga
  385. to tree and hcodegen and cleanup of hcodegen
  386. * assembling .. output reduced for smartlinking ;)
  387. Revision 1.3 1998/05/06 08:38:40 pierre
  388. * better position info with UseTokenInfo
  389. UseTokenInfo greatly simplified
  390. + added check for changed tree after first time firstpass
  391. (if we could remove all the cases were it happen
  392. we could skip all firstpass if firstpasscount > 1)
  393. Only with ExtDebug
  394. Revision 1.2 1998/04/29 10:33:53 pierre
  395. + added some code for ansistring (not complete nor working yet)
  396. * corrected operator overloading
  397. * corrected nasm output
  398. + started inline procedures
  399. + added starstarn : use ** for exponentiation (^ gave problems)
  400. + started UseTokenInfo cond to get accurate positions
  401. }