hcodegen.pas 18 KB

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