hcodegen.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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
  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. { message calls with codegenerror support }
  98. procedure message(const t : tmsgconst);
  99. procedure message1(const t : tmsgconst;const s : string);
  100. procedure message2(const t : tmsgconst;const s1,s2 : string);
  101. procedure message3(const t : tmsgconst;const s1,s2,s3 : string);
  102. { initialize respectively terminates the code generator }
  103. { for a new module or procedure }
  104. procedure codegen_doneprocedure;
  105. procedure codegen_donemodule;
  106. procedure codegen_newmodule;
  107. procedure codegen_newprocedure;
  108. { counts the labels }
  109. function case_count_labels(root : pcaserecord) : longint;
  110. { searches the highest label }
  111. function case_get_max(root : pcaserecord) : longint;
  112. { searches the lowest label }
  113. function case_get_min(root : pcaserecord) : longint;
  114. { concates/inserts the ASCII string to the data segment }
  115. procedure generate_ascii(const hs : string);
  116. procedure generate_ascii_insert(const hs : string);
  117. { concates/inserts the ASCII string from pchar to the data segment }
  118. { WARNING : if hs has no #0 and strlen(hs)=length }
  119. { the terminal zero is not written }
  120. procedure generate_pascii(a : paasmoutput;hs : pchar;length : longint);
  121. procedure generate_pascii_insert(hs : pchar;length : longint);
  122. { convert/concats a label for constants in the consts section }
  123. function constlabel2str(l : plabel;ctype:tconsttype):string;
  124. function constlabelnb2str(pnb : longint;ctype:tconsttype):string;
  125. procedure concat_constlabel(p:plabel;ctype:tconsttype);
  126. { to be able to force to have a global label for const }
  127. const
  128. make_const_global : boolean = false;
  129. implementation
  130. uses
  131. systems,comphook,cobjects,globals,files,strings;
  132. {*****************************************************************************
  133. override the message calls to set codegenerror
  134. *****************************************************************************}
  135. procedure message(const t : tmsgconst);
  136. var
  137. olderrorcount : longint;
  138. begin
  139. if not(codegenerror) then
  140. begin
  141. olderrorcount:=status.errorcount;
  142. verbose.Message(t);
  143. codegenerror:=olderrorcount<>status.errorcount;
  144. end;
  145. end;
  146. procedure message1(const t : tmsgconst;const s : string);
  147. var
  148. olderrorcount : longint;
  149. begin
  150. if not(codegenerror) then
  151. begin
  152. olderrorcount:=status.errorcount;
  153. verbose.Message1(t,s);
  154. codegenerror:=olderrorcount<>status.errorcount;
  155. end;
  156. end;
  157. procedure message2(const t : tmsgconst;const s1,s2 : string);
  158. var
  159. olderrorcount : longint;
  160. begin
  161. if not(codegenerror) then
  162. begin
  163. olderrorcount:=status.errorcount;
  164. verbose.Message2(t,s1,s2);
  165. codegenerror:=olderrorcount<>status.errorcount;
  166. end;
  167. end;
  168. procedure message3(const t : tmsgconst;const s1,s2,s3 : string);
  169. var
  170. olderrorcount : longint;
  171. begin
  172. if not(codegenerror) then
  173. begin
  174. olderrorcount:=status.errorcount;
  175. verbose.Message3(t,s1,s2,s3);
  176. codegenerror:=olderrorcount<>status.errorcount;
  177. end;
  178. end;
  179. {*****************************************************************************
  180. initialize/terminate the codegen for procedure and modules
  181. *****************************************************************************}
  182. procedure codegen_newprocedure;
  183. begin
  184. aktbreaklabel:=nil;
  185. aktcontinuelabel:=nil;
  186. { aktexitlabel:=0; is store in oldaktexitlabel
  187. so it must not be reset to zero before this storage !}
  188. { the type of this lists isn't important }
  189. { because the code of this lists is }
  190. { copied to the code segment }
  191. procinfo.aktentrycode:=new(paasmoutput,init);
  192. procinfo.aktexitcode:=new(paasmoutput,init);
  193. procinfo.aktproccode:=new(paasmoutput,init);
  194. procinfo.aktlocaldata:=new(paasmoutput,init);
  195. end;
  196. procedure codegen_doneprocedure;
  197. begin
  198. dispose(procinfo.aktentrycode,done);
  199. dispose(procinfo.aktexitcode,done);
  200. dispose(procinfo.aktproccode,done);
  201. dispose(procinfo.aktlocaldata,done);
  202. end;
  203. procedure codegen_newmodule;
  204. begin
  205. exprasmlist:=new(paasmoutput,init);
  206. datasegment:=new(paasmoutput,init);
  207. codesegment:=new(paasmoutput,init);
  208. bsssegment:=new(paasmoutput,init);
  209. debuglist:=new(paasmoutput,init);
  210. externals:=new(paasmoutput,init);
  211. internals:=new(paasmoutput,init);
  212. consts:=new(paasmoutput,init);
  213. rttilist:=new(paasmoutput,init);
  214. importssection:=nil;
  215. exportssection:=nil;
  216. resourcesection:=nil;
  217. end;
  218. procedure codegen_donemodule;
  219. begin
  220. dispose(exprasmlist,done);
  221. dispose(codesegment,done);
  222. dispose(bsssegment,done);
  223. dispose(datasegment,done);
  224. dispose(debuglist,done);
  225. dispose(externals,done);
  226. dispose(consts,done);
  227. dispose(rttilist,done);
  228. if assigned(importssection) then
  229. dispose(importssection,done);
  230. if assigned(exportssection) then
  231. dispose(exportssection,done);
  232. if assigned(resourcesection) then
  233. dispose(resourcesection,done);
  234. end;
  235. {*****************************************************************************
  236. Case Helpers
  237. *****************************************************************************}
  238. function case_count_labels(root : pcaserecord) : longint;
  239. var
  240. _l : longint;
  241. procedure count(p : pcaserecord);
  242. begin
  243. inc(_l);
  244. if assigned(p^.less) then
  245. count(p^.less);
  246. if assigned(p^.greater) then
  247. count(p^.greater);
  248. end;
  249. begin
  250. _l:=0;
  251. count(root);
  252. case_count_labels:=_l;
  253. end;
  254. function case_get_max(root : pcaserecord) : longint;
  255. var
  256. hp : pcaserecord;
  257. begin
  258. hp:=root;
  259. while assigned(hp^.greater) do
  260. hp:=hp^.greater;
  261. case_get_max:=hp^._high;
  262. end;
  263. function case_get_min(root : pcaserecord) : longint;
  264. var
  265. hp : pcaserecord;
  266. begin
  267. hp:=root;
  268. while assigned(hp^.less) do
  269. hp:=hp^.less;
  270. case_get_min:=hp^._low;
  271. end;
  272. {*****************************************************************************
  273. String Helpers
  274. *****************************************************************************}
  275. procedure generate_ascii(const hs : string);
  276. begin
  277. datasegment^.concat(new(pai_string,init(hs)))
  278. end;
  279. procedure generate_ascii_insert(const hs : string);
  280. begin
  281. datasegment^.insert(new(pai_string,init(hs)));
  282. end;
  283. function strnew(p : pchar;length : longint) : pchar;
  284. var
  285. pc : pchar;
  286. begin
  287. getmem(pc,length);
  288. move(p^,pc^,length);
  289. strnew:=pc;
  290. end;
  291. { concates the ASCII string from pchar to the asmslist a }
  292. procedure generate_pascii(a : paasmoutput;hs : pchar;length : longint);
  293. var
  294. real_end,current_begin,current_end : pchar;
  295. c :char;
  296. begin
  297. if assigned(hs) then
  298. begin
  299. current_begin:=hs;
  300. real_end:=strend(hs);
  301. c:=hs[0];
  302. while length>32 do
  303. begin
  304. { restore the char displaced }
  305. current_begin[0]:=c;
  306. current_end:=current_begin+32;
  307. { store the char for next loop }
  308. c:=current_end[0];
  309. current_end[0]:=#0;
  310. a^.concat(new(pai_string,init_length_pchar(strnew(current_begin,32),32)));
  311. current_begin:=current_end;
  312. length:=length-32;
  313. end;
  314. current_begin[0]:=c;
  315. a^.concat(new(pai_string,init_length_pchar(strnew(current_begin,length),length)));
  316. end;
  317. end;
  318. { inserts the ASCII string from pchar to the const segment }
  319. procedure generate_pascii_insert(hs : pchar;length : longint);
  320. var
  321. real_end,current_begin,current_end : pchar;
  322. c :char;
  323. begin
  324. if assigned(hs) then
  325. begin
  326. current_begin:=hs;
  327. real_end:=strend(hs);
  328. c:=hs[0];
  329. length:=longint(real_end)-longint(hs);
  330. while length>32 do
  331. begin
  332. { restore the char displaced }
  333. current_begin[0]:=c;
  334. current_end:=current_begin+32;
  335. { store the char for next loop }
  336. c:=current_end[0];
  337. current_end[0]:=#0;
  338. datasegment^.insert(new(pai_string,init_length_pchar(strnew(current_begin,32),32)));
  339. length:=length-32;
  340. end;
  341. datasegment^.insert(new(pai_string,init_length_pchar(strnew(current_begin,length),length)));
  342. end;
  343. end;
  344. {*****************************************************************************
  345. Const Helpers
  346. *****************************************************************************}
  347. const
  348. consttypestr : array[tconsttype] of string[6]=
  349. ('ord','string','real','bool','int','char','set');
  350. { Peter this gives problems for my inlines !! }
  351. { we must use the number directly !!! (PM) }
  352. function constlabel2str(l : plabel;ctype:tconsttype):string;
  353. begin
  354. if (cs_smartlink in aktmoduleswitches) or
  355. make_const_global {or (aktoutputformat in [as_tasm])} then
  356. constlabel2str:='_$'+current_module^.modulename^+'$'+consttypestr[ctype]+'_const_'+tostr(l^.nb)
  357. else
  358. constlabel2str:=lab2str(l);
  359. end;
  360. function constlabelnb2str(pnb : longint;ctype:tconsttype):string;
  361. begin
  362. if (cs_smartlink in aktmoduleswitches) or
  363. make_const_global {or (aktoutputformat in [as_tasm])} then
  364. constlabelnb2str:='_$'+current_module^.modulename^+'$'+consttypestr[ctype]+'_const_'+tostr(pnb)
  365. else
  366. constlabelnb2str:=target_asm.labelprefix+tostr(pnb);
  367. end;
  368. procedure concat_constlabel(p:plabel;ctype:tconsttype);
  369. var
  370. s : string;
  371. begin
  372. if (cs_smartlink in aktmoduleswitches) or
  373. make_const_global {or (aktoutputformat in [as_tasm])} then
  374. begin
  375. s:='_$'+current_module^.modulename^+'$'+consttypestr[ctype]+'_const_'+tostr(p^.nb);
  376. if (cs_smartlink in aktmoduleswitches) then
  377. begin
  378. consts^.concat(new(pai_cut,init));
  379. consts^.concat(new(pai_symbol,init_global(s)))
  380. end
  381. else
  382. consts^.concat(new(pai_symbol,init_global(s)));
  383. end
  384. else
  385. consts^.concat(new(pai_label,init(p)));
  386. end;
  387. end.
  388. {
  389. $Log$
  390. Revision 1.15 1998-09-01 09:02:51 peter
  391. * moved message() to hcodegen, so pass_2 also uses them
  392. Revision 1.14 1998/08/21 14:08:43 pierre
  393. + TEST_FUNCRET now default (old code removed)
  394. works also for m68k (at least compiles)
  395. Revision 1.13 1998/08/20 09:26:38 pierre
  396. + funcret setting in underproc testing
  397. compile with _dTEST_FUNCRET
  398. Revision 1.12 1998/08/10 14:50:01 peter
  399. + localswitches, moduleswitches, globalswitches splitting
  400. Revision 1.11 1998/07/28 21:52:51 florian
  401. + implementation of raise and try..finally
  402. + some misc. exception stuff
  403. Revision 1.10 1998/07/20 18:40:13 florian
  404. * handling of ansi string constants should now work
  405. Revision 1.9 1998/06/05 16:13:34 pierre
  406. * fix for real and string consts inside inlined procs
  407. Revision 1.8 1998/06/04 23:51:40 peter
  408. * m68k compiles
  409. + .def file creation moved to gendef.pas so it could also be used
  410. for win32
  411. Revision 1.7 1998/06/04 09:55:38 pierre
  412. * demangled name of procsym reworked to become independant of the mangling scheme
  413. Revision 1.6 1998/05/23 01:21:08 peter
  414. + aktasmmode, aktoptprocessor, aktoutputformat
  415. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  416. + $LIBNAME to set the library name where the unit will be put in
  417. * splitted cgi386 a bit (codeseg to large for bp7)
  418. * nasm, tasm works again. nasm moved to ag386nsm.pas
  419. Revision 1.5 1998/05/20 09:42:34 pierre
  420. + UseTokenInfo now default
  421. * unit in interface uses and implementation uses gives error now
  422. * only one error for unknown symbol (uses lastsymknown boolean)
  423. the problem came from the label code !
  424. + first inlined procedures and function work
  425. (warning there might be allowed cases were the result is still wrong !!)
  426. * UseBrower updated gives a global list of all position of all used symbols
  427. with switch -gb
  428. Revision 1.4 1998/05/07 00:17:01 peter
  429. * smartlinking for sets
  430. + consts labels are now concated/generated in hcodegen
  431. * moved some cpu code to cga and some none cpu depended code from cga
  432. to tree and hcodegen and cleanup of hcodegen
  433. * assembling .. output reduced for smartlinking ;)
  434. Revision 1.3 1998/05/06 08:38:40 pierre
  435. * better position info with UseTokenInfo
  436. UseTokenInfo greatly simplified
  437. + added check for changed tree after first time firstpass
  438. (if we could remove all the cases were it happen
  439. we could skip all firstpass if firstpasscount > 1)
  440. Only with ExtDebug
  441. Revision 1.2 1998/04/29 10:33:53 pierre
  442. + added some code for ansistring (not complete nor working yet)
  443. * corrected operator overloading
  444. * corrected nasm output
  445. + started inline procedures
  446. + added starstarn : use ** for exponentiation (^ gave problems)
  447. + started UseTokenInfo cond to get accurate positions
  448. }