hcodegen.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. cobjects,
  22. tokens,verbose,
  23. aasm,symtable,cpubase;
  24. const
  25. pi_uses_asm = $1; { set, if the procedure uses asm }
  26. pi_is_global = $2; { set, if the procedure is exported by an unit }
  27. pi_do_call = $4; { set, if the procedure does a call }
  28. pi_operator = $8; { set, if the procedure is an operator }
  29. pi_C_import = $10; { set, if the procedure is an external C function }
  30. pi_uses_exceptions = $20;{ set, if the procedure has a try statement => }
  31. { no register variables }
  32. pi_is_assembler = $40; { set if the procedure is declared as ASSEMBLER
  33. => don't optimize}
  34. pi_needs_implicit_finally = $80; { set, if the procedure contains data which }
  35. { needs to be finalized }
  36. type
  37. pprocinfo = ^tprocinfo;
  38. tprocinfo = record
  39. { pointer to parent in nested procedures }
  40. parent : pprocinfo;
  41. { current class, if we are in a method }
  42. _class : pobjectdef;
  43. { return type }
  44. retdef : pdef;
  45. { return type }
  46. sym : pprocsym;
  47. { symbol of the function, and the sym for result variable }
  48. resultfuncretsym,
  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. ptemptodestroy = ^ttemptodestroy;
  82. ttemptodestroy = object(tlinkedlist_item)
  83. typ : pdef;
  84. address : treference;
  85. constructor init(const a : treference;p : pdef);
  86. end;
  87. var
  88. { info about the current sub routine }
  89. procinfo : pprocinfo;
  90. { labels for BREAK and CONTINUE }
  91. aktbreaklabel,aktcontinuelabel : pasmlabel;
  92. { label when the result is true or false }
  93. truelabel,falselabel : pasmlabel;
  94. { label to leave the sub routine }
  95. aktexitlabel : pasmlabel;
  96. { also an exit label, only used we need to clear only the stack }
  97. aktexit2label : pasmlabel;
  98. { only used in constructor for fail or if getmem fails }
  99. faillabel,quickexitlabel : pasmlabel;
  100. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  101. simple_loadn : boolean;
  102. { tries to hold the amount of times which the current tree is processed }
  103. t_times : longint;
  104. { true, if an error while code generation occurs }
  105. codegenerror : boolean;
  106. { save the size of pushed parameter, needed for aligning }
  107. pushedparasize : longint;
  108. make_const_global : boolean;
  109. { message calls with codegenerror support }
  110. procedure cgmessage(t : tmsgconst);
  111. procedure cgmessage1(t : tmsgconst;const s : string);
  112. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  113. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  114. procedure CGMessagePos(const pos:tfileposinfo;t:tmsgconst);
  115. procedure CGMessagePos1(const pos:tfileposinfo;t:tmsgconst;const s1:string);
  116. procedure CGMessagePos2(const pos:tfileposinfo;t:tmsgconst;const s1,s2:string);
  117. procedure CGMessagePos3(const pos:tfileposinfo;t:tmsgconst;const s1,s2,s3:string);
  118. { initialize respectively terminates the code generator }
  119. { for a new module or procedure }
  120. procedure codegen_doneprocedure;
  121. procedure codegen_donemodule;
  122. procedure codegen_newmodule;
  123. procedure codegen_newprocedure;
  124. implementation
  125. uses
  126. systems,globals,files,strings,cresstr;
  127. {*****************************************************************************
  128. override the message calls to set codegenerror
  129. *****************************************************************************}
  130. procedure cgmessage(t : tmsgconst);
  131. var
  132. olderrorcount : longint;
  133. begin
  134. if not(codegenerror) then
  135. begin
  136. olderrorcount:=Errorcount;
  137. verbose.Message(t);
  138. codegenerror:=olderrorcount<>Errorcount;
  139. end;
  140. end;
  141. procedure cgmessage1(t : tmsgconst;const s : string);
  142. var
  143. olderrorcount : longint;
  144. begin
  145. if not(codegenerror) then
  146. begin
  147. olderrorcount:=Errorcount;
  148. verbose.Message1(t,s);
  149. codegenerror:=olderrorcount<>Errorcount;
  150. end;
  151. end;
  152. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  153. var
  154. olderrorcount : longint;
  155. begin
  156. if not(codegenerror) then
  157. begin
  158. olderrorcount:=Errorcount;
  159. verbose.Message2(t,s1,s2);
  160. codegenerror:=olderrorcount<>Errorcount;
  161. end;
  162. end;
  163. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  164. var
  165. olderrorcount : longint;
  166. begin
  167. if not(codegenerror) then
  168. begin
  169. olderrorcount:=Errorcount;
  170. verbose.Message3(t,s1,s2,s3);
  171. codegenerror:=olderrorcount<>Errorcount;
  172. end;
  173. end;
  174. procedure cgmessagepos(const pos:tfileposinfo;t : tmsgconst);
  175. var
  176. olderrorcount : longint;
  177. begin
  178. if not(codegenerror) then
  179. begin
  180. olderrorcount:=Errorcount;
  181. verbose.MessagePos(pos,t);
  182. codegenerror:=olderrorcount<>Errorcount;
  183. end;
  184. end;
  185. procedure cgmessagepos1(const pos:tfileposinfo;t : tmsgconst;const s : string);
  186. var
  187. olderrorcount : longint;
  188. begin
  189. if not(codegenerror) then
  190. begin
  191. olderrorcount:=Errorcount;
  192. verbose.MessagePos1(pos,t,s);
  193. codegenerror:=olderrorcount<>Errorcount;
  194. end;
  195. end;
  196. procedure cgmessagepos2(const pos:tfileposinfo;t : tmsgconst;const s1,s2 : string);
  197. var
  198. olderrorcount : longint;
  199. begin
  200. if not(codegenerror) then
  201. begin
  202. olderrorcount:=Errorcount;
  203. verbose.MessagePos2(pos,t,s1,s2);
  204. codegenerror:=olderrorcount<>Errorcount;
  205. end;
  206. end;
  207. procedure cgmessagepos3(const pos:tfileposinfo;t : tmsgconst;const s1,s2,s3 : string);
  208. var
  209. olderrorcount : longint;
  210. begin
  211. if not(codegenerror) then
  212. begin
  213. olderrorcount:=Errorcount;
  214. verbose.MessagePos3(pos,t,s1,s2,s3);
  215. codegenerror:=olderrorcount<>Errorcount;
  216. end;
  217. end;
  218. {*****************************************************************************
  219. initialize/terminate the codegen for procedure and modules
  220. *****************************************************************************}
  221. procedure codegen_newprocedure;
  222. begin
  223. aktbreaklabel:=nil;
  224. aktcontinuelabel:=nil;
  225. { aktexitlabel:=0; is store in oldaktexitlabel
  226. so it must not be reset to zero before this storage !}
  227. { new procinfo }
  228. new(procinfo);
  229. fillchar(procinfo^,sizeof(tprocinfo),0);
  230. { the type of this lists isn't important }
  231. { because the code of this lists is }
  232. { copied to the code segment }
  233. procinfo^.aktentrycode:=new(paasmoutput,init);
  234. procinfo^.aktexitcode:=new(paasmoutput,init);
  235. procinfo^.aktproccode:=new(paasmoutput,init);
  236. procinfo^.aktlocaldata:=new(paasmoutput,init);
  237. end;
  238. procedure codegen_doneprocedure;
  239. begin
  240. dispose(procinfo^.aktentrycode,done);
  241. dispose(procinfo^.aktexitcode,done);
  242. dispose(procinfo^.aktproccode,done);
  243. dispose(procinfo^.aktlocaldata,done);
  244. dispose(procinfo);
  245. procinfo:=nil;
  246. end;
  247. procedure codegen_newmodule;
  248. begin
  249. exprasmlist:=new(paasmoutput,init);
  250. datasegment:=new(paasmoutput,init);
  251. codesegment:=new(paasmoutput,init);
  252. bsssegment:=new(paasmoutput,init);
  253. debuglist:=new(paasmoutput,init);
  254. consts:=new(paasmoutput,init);
  255. rttilist:=new(paasmoutput,init);
  256. importssection:=nil;
  257. exportssection:=nil;
  258. resourcesection:=nil;
  259. { assembler symbols }
  260. asmsymbollist:=new(pasmsymbollist,init);
  261. asmsymbollist^.usehash;
  262. end;
  263. procedure codegen_donemodule;
  264. {$ifdef MEMDEBUG}
  265. var
  266. d : tmemdebug;
  267. {$endif}
  268. begin
  269. {$ifdef MEMDEBUG}
  270. d.init('asmlist');
  271. {$endif}
  272. dispose(exprasmlist,done);
  273. dispose(codesegment,done);
  274. dispose(bsssegment,done);
  275. dispose(datasegment,done);
  276. dispose(debuglist,done);
  277. dispose(consts,done);
  278. dispose(rttilist,done);
  279. if assigned(importssection) then
  280. dispose(importssection,done);
  281. if assigned(exportssection) then
  282. dispose(exportssection,done);
  283. if assigned(resourcesection) then
  284. dispose(resourcesection,done);
  285. {$ifdef MEMDEBUG}
  286. d.done;
  287. {$endif}
  288. { assembler symbols }
  289. {$ifdef MEMDEBUG}
  290. d.init('asmsymbol');
  291. {$endif}
  292. dispose(asmsymbollist,done);
  293. {$ifdef MEMDEBUG}
  294. d.done;
  295. {$endif}
  296. { resourcestrings }
  297. ResetResourceStrings;
  298. end;
  299. {*****************************************************************************
  300. TTempToDestroy
  301. *****************************************************************************}
  302. constructor ttemptodestroy.init(const a : treference;p : pdef);
  303. begin
  304. inherited init;
  305. address:=a;
  306. typ:=p;
  307. end;
  308. end.
  309. {
  310. $Log$
  311. Revision 1.44 1999-10-13 10:42:15 peter
  312. * cgmessagepos functions
  313. Revision 1.43 1999/09/27 23:44:51 peter
  314. * procinfo is now a pointer
  315. * support for result setting in sub procedure
  316. Revision 1.42 1999/08/26 20:24:40 michael
  317. + Hopefuly last fixes for resourcestrings
  318. Revision 1.41 1999/08/24 13:14:03 peter
  319. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  320. Revision 1.40 1999/08/24 12:01:32 michael
  321. + changes for resourcestrings
  322. Revision 1.39 1999/08/19 13:10:18 pierre
  323. + faillabel for _FAIL
  324. Revision 1.38 1999/08/16 18:23:56 peter
  325. * reset resourcestringlist in newmodule.
  326. Revision 1.37 1999/08/04 00:23:02 florian
  327. * renamed i386asm and i386base to cpuasm and cpubase
  328. Revision 1.36 1999/08/01 23:09:26 michael
  329. * procbase -> cpubase
  330. Revision 1.35 1999/08/01 23:04:49 michael
  331. + Changes for Alpha
  332. Revision 1.34 1999/07/22 09:37:42 florian
  333. + resourcestring implemented
  334. + start of longstring support
  335. Revision 1.33 1999/05/27 19:44:31 peter
  336. * removed oldasm
  337. * plabel -> pasmlabel
  338. * -a switches to source writing automaticly
  339. * assembler readers OOPed
  340. * asmsymbol automaticly external
  341. * jumptables and other label fixes for asm readers
  342. Revision 1.32 1999/05/21 13:55:01 peter
  343. * NEWLAB for label as symbol
  344. Revision 1.31 1999/05/17 21:57:08 florian
  345. * new temporary ansistring handling
  346. Revision 1.30 1999/05/01 13:24:22 peter
  347. * merged nasm compiler
  348. * old asm moved to oldasm/
  349. Revision 1.29 1999/04/21 09:43:38 peter
  350. * storenumber works
  351. * fixed some typos in double_checksum
  352. + incompatible types type1 and type2 message (with storenumber)
  353. Revision 1.28 1999/03/24 23:17:00 peter
  354. * fixed bugs 212,222,225,227,229,231,233
  355. Revision 1.27 1999/02/25 21:02:37 peter
  356. * ag386bin updates
  357. + coff writer
  358. Revision 1.26 1999/02/22 02:15:21 peter
  359. * updates for ag386bin
  360. Revision 1.25 1999/01/21 22:10:45 peter
  361. * fixed array of const
  362. * generic platform independent high() support
  363. Revision 1.24 1998/12/29 18:48:18 jonas
  364. + optimize pascal code surrounding assembler blocks
  365. Revision 1.23 1998/11/27 14:50:38 peter
  366. + open strings, $P switch support
  367. Revision 1.22 1998/11/16 12:12:21 peter
  368. - generate_pascii which is obsolete
  369. Revision 1.21 1998/11/04 10:11:38 peter
  370. * ansistring fixes
  371. Revision 1.20 1998/10/29 15:42:48 florian
  372. + partial disposing of temp. ansistrings
  373. Revision 1.19 1998/10/26 22:58:18 florian
  374. * new introduded problem with classes fix, the parent class wasn't set
  375. correct, if the class was defined forward before
  376. Revision 1.18 1998/10/06 17:16:50 pierre
  377. * some memory leaks fixed (thanks to Peter for heaptrc !)
  378. Revision 1.17 1998/09/17 09:42:37 peter
  379. + pass_2 for cg386
  380. * Message() -> CGMessage() for pass_1/pass_2
  381. Revision 1.16 1998/09/07 18:46:04 peter
  382. * update smartlinking, uses getdatalabel
  383. * renamed ptree.value vars to value_str,value_real,value_set
  384. Revision 1.15 1998/09/01 09:02:51 peter
  385. * moved message() to hcodegen, so pass_2 also uses them
  386. Revision 1.14 1998/08/21 14:08:43 pierre
  387. + TEST_FUNCRET now default (old code removed)
  388. works also for m68k (at least compiles)
  389. Revision 1.13 1998/08/20 09:26:38 pierre
  390. + funcret setting in underproc testing
  391. compile with _dTEST_FUNCRET
  392. Revision 1.12 1998/08/10 14:50:01 peter
  393. + localswitches, moduleswitches, globalswitches splitting
  394. Revision 1.11 1998/07/28 21:52:51 florian
  395. + implementation of raise and try..finally
  396. + some misc. exception stuff
  397. Revision 1.10 1998/07/20 18:40:13 florian
  398. * handling of ansi string constants should now work
  399. Revision 1.9 1998/06/05 16:13:34 pierre
  400. * fix for real and string consts inside inlined procs
  401. Revision 1.8 1998/06/04 23:51:40 peter
  402. * m68k compiles
  403. + .def file creation moved to gendef.pas so it could also be used
  404. for win32
  405. Revision 1.7 1998/06/04 09:55:38 pierre
  406. * demangled name of procsym reworked to become independant of the mangling scheme
  407. Revision 1.6 1998/05/23 01:21:08 peter
  408. + aktasmmode, aktoptprocessor, aktoutputformat
  409. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  410. + $LIBNAME to set the library name where the unit will be put in
  411. * splitted cgi386 a bit (codeseg to large for bp7)
  412. * nasm, tasm works again. nasm moved to ag386nsm.pas
  413. Revision 1.5 1998/05/20 09:42:34 pierre
  414. + UseTokenInfo now default
  415. * unit in interface uses and implementation uses gives error now
  416. * only one error for unknown symbol (uses lastsymknown boolean)
  417. the problem came from the label code !
  418. + first inlined procedures and function work
  419. (warning there might be allowed cases were the result is still wrong !!)
  420. * UseBrower updated gives a global list of all position of all used symbols
  421. with switch -gb
  422. Revision 1.4 1998/05/07 00:17:01 peter
  423. * smartlinking for sets
  424. + consts labels are now concated/generated in hcodegen
  425. * moved some cpu code to cga and some none cpu depended code from cga
  426. to tree and hcodegen and cleanup of hcodegen
  427. * assembling .. output reduced for smartlinking ;)
  428. Revision 1.3 1998/05/06 08:38:40 pierre
  429. * better position info with UseTokenInfo
  430. UseTokenInfo greatly simplified
  431. + added check for changed tree after first time firstpass
  432. (if we could remove all the cases were it happen
  433. we could skip all firstpass if firstpasscount > 1)
  434. Only with ExtDebug
  435. Revision 1.2 1998/04/29 10:33:53 pierre
  436. + added some code for ansistring (not complete nor working yet)
  437. * corrected operator overloading
  438. * corrected nasm output
  439. + started inline procedures
  440. + added starstarn : use ** for exponentiation (^ gave problems)
  441. + started UseTokenInfo cond to get accurate positions
  442. }