compiler.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit is the interface of the compiler which can be used by
  5. external programs to link in the compiler
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************}
  18. unit compiler;
  19. {$i fpcdefs.inc}
  20. {$ifdef FPC}
  21. { One of Alpha, I386 or M68K must be defined }
  22. {$UNDEF CPUOK}
  23. {$ifdef I386}
  24. {$define CPUOK}
  25. {$endif}
  26. {$ifdef M68K}
  27. {$ifndef CPUOK}
  28. {$DEFINE CPUOK}
  29. {$else}
  30. {$fatal cannot define two CPU switches}
  31. {$endif}
  32. {$endif}
  33. {$ifdef alpha}
  34. {$ifndef CPUOK}
  35. {$DEFINE CPUOK}
  36. {$else}
  37. {$fatal cannot define two CPU switches}
  38. {$endif}
  39. {$endif}
  40. {$ifdef vis}
  41. {$ifndef CPUOK}
  42. {$DEFINE CPUOK}
  43. {$else}
  44. {$fatal cannot define two CPU switches}
  45. {$endif}
  46. {$endif}
  47. {$ifdef powerpc}
  48. {$ifndef CPUOK}
  49. {$DEFINE CPUOK}
  50. {$else}
  51. {$fatal cannot define two CPU switches}
  52. {$endif}
  53. {$endif}
  54. {$ifdef ia64}
  55. {$ifndef CPUOK}
  56. {$DEFINE CPUOK}
  57. {$else}
  58. {$fatal cannot define two CPU switches}
  59. {$endif}
  60. {$endif}
  61. {$ifdef SPARC}
  62. {$ifndef CPUOK}
  63. {$DEFINE CPUOK}
  64. {$else}
  65. {$fatal cannot define two CPU switches}
  66. {$endif}
  67. {$endif}
  68. {$ifdef x86_64}
  69. {$ifndef CPUOK}
  70. {$DEFINE CPUOK}
  71. {$else}
  72. {$fatal cannot define two CPU switches}
  73. {$endif}
  74. {$endif}
  75. {$ifdef ARM}
  76. {$ifndef CPUOK}
  77. {$DEFINE CPUOK}
  78. {$else}
  79. {$fatal cannot define two CPU switches}
  80. {$endif ARM}
  81. {$endif ARM}
  82. {$ifndef CPUOK}
  83. {$fatal One of the switches I386, iA64, Alpha, PowerPC or M68K must be defined}
  84. {$endif}
  85. {$ifdef support_mmx}
  86. {$ifndef i386}
  87. {$fatal I386 switch must be on for MMX support}
  88. {$endif i386}
  89. {$endif support_mmx}
  90. {$endif}
  91. interface
  92. uses
  93. {$ifdef fpc}
  94. {$ifdef GO32V2}
  95. emu387,
  96. {$endif GO32V2}
  97. {$endif}
  98. {$ifdef USEEXCEPT}
  99. tpexcept,
  100. {$endif USEEXCEPT}
  101. {$ifdef BrowserLog}
  102. browlog,
  103. {$endif BrowserLog}
  104. {$ifdef Delphi}
  105. dmisc,
  106. {$else Delphi}
  107. dos,
  108. {$endif Delphi}
  109. verbose,comphook,systems,
  110. cutils,cclasses,globals,options,fmodule,parser,symtable,
  111. assemble,link,import,export,tokens,pass_1
  112. { cpu overrides }
  113. ,cpuswtch
  114. { cpu codegenerator }
  115. ,cgcpu
  116. {$ifndef NOPASS2}
  117. ,cpunode
  118. {$endif}
  119. { cpu targets }
  120. ,cputarg
  121. { cpu parameter handling }
  122. ,cpupara
  123. { procinfo stuff }
  124. ,cpupi
  125. { system information for source system }
  126. { the information about the target os }
  127. { are pulled in by the t_* units }
  128. {$ifdef amiga}
  129. ,i_amiga
  130. {$endif amiga}
  131. {$ifdef atari}
  132. ,i_atari
  133. {$endif atari}
  134. {$ifdef beos}
  135. ,i_beos
  136. {$endif beos}
  137. {$ifdef fbds}
  138. ,i_fbsd
  139. {$endif fbds}
  140. {$ifdef go32v2}
  141. ,i_go32v2
  142. {$endif go32v2}
  143. {$ifdef linux}
  144. ,i_linux
  145. {$endif linux}
  146. {$ifdef macos}
  147. ,i_macos
  148. {$endif macos}
  149. {$ifdef nwm}
  150. ,i_nwm
  151. {$endif nwm}
  152. {$ifdef os2}
  153. {$ifdef emx}
  154. ,i_emx
  155. {$else emx}
  156. ,i_os2
  157. {$endif emx}
  158. {$endif os2}
  159. {$ifdef palmos}
  160. ,i_palmos
  161. {$endif palmos}
  162. {$ifdef sunos}
  163. ,i_sunos
  164. {$endif sunos}
  165. {$ifdef wdosx}
  166. ,i_wdosx
  167. {$endif wdosx}
  168. {$ifdef win32}
  169. ,i_win32
  170. {$endif win32}
  171. ;
  172. function Compile(const cmd:string):longint;
  173. implementation
  174. uses
  175. aasmcpu;
  176. {$ifdef EXTDEBUG}
  177. {$define SHOWUSEDMEM}
  178. {$endif}
  179. {$ifdef MEMDEBUG}
  180. {$define SHOWUSEDMEM}
  181. {$endif}
  182. var
  183. CompilerInitedAfterArgs,
  184. CompilerInited : boolean;
  185. olddo_stop : tstopprocedure;
  186. {$ifdef USEEXCEPT}
  187. procedure RecoverStop;
  188. begin
  189. if recoverpospointer<>nil then
  190. LongJmp(recoverpospointer^,1)
  191. else
  192. Do_Halt(1);
  193. end;
  194. {$endif USEEXCEPT}
  195. {****************************************************************************
  196. Compiler
  197. ****************************************************************************}
  198. procedure DoneCompiler;
  199. begin
  200. if not CompilerInited then
  201. exit;
  202. { Free compiler if args are read }
  203. {$ifdef BrowserLog}
  204. DoneBrowserLog;
  205. {$endif BrowserLog}
  206. {$ifdef BrowserCol}
  207. do_doneSymbolInfo;
  208. {$endif BrowserCol}
  209. if CompilerInitedAfterArgs then
  210. begin
  211. CompilerInitedAfterArgs:=false;
  212. DoneParser;
  213. DoneImport;
  214. DoneExport;
  215. DoneLinker;
  216. DoneAssembler;
  217. DoneAsm;
  218. end;
  219. { Free memory for the others }
  220. CompilerInited:=false;
  221. DoneSymtable;
  222. DoneGlobals;
  223. donetokens;
  224. {$ifdef USEEXCEPT}
  225. recoverpospointer:=nil;
  226. longjump_used:=false;
  227. {$endif USEEXCEPT}
  228. end;
  229. procedure InitCompiler(const cmd:string);
  230. begin
  231. if CompilerInited then
  232. DoneCompiler;
  233. { inits which need to be done before the arguments are parsed }
  234. InitSystems;
  235. { globals depends on source_info so it must be after systems }
  236. InitGlobals;
  237. { verbose depends on exe_path and must be after globals }
  238. InitVerbose;
  239. {$ifdef BrowserLog}
  240. InitBrowserLog;
  241. {$endif BrowserLog}
  242. {$ifdef BrowserCol}
  243. do_initSymbolInfo;
  244. {$endif BrowserCol}
  245. inittokens;
  246. InitSymtable;
  247. CompilerInited:=true;
  248. { this is needed here for the IDE
  249. in case of compilation failure
  250. at the previous compile }
  251. current_module:=nil;
  252. { read the arguments }
  253. read_arguments(cmd);
  254. { inits which depend on arguments }
  255. InitParser;
  256. InitImport;
  257. InitExport;
  258. InitLinker;
  259. InitAssembler;
  260. InitAsm;
  261. CompilerInitedAfterArgs:=true;
  262. end;
  263. procedure minimal_stop;
  264. begin
  265. DoneCompiler;
  266. olddo_stop{$ifdef FPCPROCVAR}(){$endif};
  267. end;
  268. function Compile(const cmd:string):longint;
  269. {$ifdef fpc}
  270. {$maxfpuregisters 0}
  271. {$endif fpc}
  272. procedure writepathlist(w:longint;l:TSearchPathList);
  273. var
  274. hp : tstringlistitem;
  275. begin
  276. hp:=tstringlistitem(l.first);
  277. while assigned(hp) do
  278. begin
  279. Message1(w,hp.str);
  280. hp:=tstringlistitem(hp.next);
  281. end;
  282. end;
  283. function getrealtime : real;
  284. var
  285. h,m,s,s100 : word;
  286. begin
  287. gettime(h,m,s,s100);
  288. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  289. end;
  290. var
  291. starttime : real;
  292. {$ifdef USEEXCEPT}
  293. recoverpos : jmp_buf;
  294. {$endif}
  295. begin
  296. olddo_stop:=do_stop;
  297. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  298. { Initialize the compiler }
  299. InitCompiler(cmd);
  300. { show some info }
  301. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  302. Message1(general_d_sourceos,source_info.name);
  303. Message1(general_i_targetos,target_info.name);
  304. Message1(general_t_exepath,exepath);
  305. WritePathList(general_t_unitpath,unitsearchpath);
  306. WritePathList(general_t_includepath,includesearchpath);
  307. WritePathList(general_t_librarypath,librarysearchpath);
  308. WritePathList(general_t_objectpath,objectsearchpath);
  309. {$ifdef USEEXCEPT}
  310. if setjmp(recoverpos)=0 then
  311. begin
  312. recoverpospointer:=@recoverpos;
  313. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  314. {$endif USEEXCEPT}
  315. starttime:=getrealtime;
  316. {$ifdef PREPROCWRITE}
  317. if parapreprocess then
  318. parser.preprocess(inputdir+inputfile+inputextension)
  319. else
  320. {$endif PREPROCWRITE}
  321. parser.compile(inputdir+inputfile+inputextension);
  322. if status.errorcount=0 then
  323. begin
  324. starttime:=getrealtime-starttime;
  325. if starttime<0 then
  326. starttime:=starttime+3600.0*24.0;
  327. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  328. '.'+tostr(trunc(frac(starttime)*10)));
  329. end;
  330. {$ifdef USEEXCEPT}
  331. end;
  332. {$endif USEEXCEPT}
  333. { Stop is always called, so we come here when a program is compiled or not }
  334. do_stop:=olddo_stop;
  335. { Stop the compiler, frees also memory }
  336. { no message possible after this !! }
  337. DoneCompiler;
  338. { Set the return value if an error has occurred }
  339. if status.errorcount=0 then
  340. Compile:=0
  341. else
  342. Compile:=1;
  343. DoneVerbose;
  344. {$ifdef SHOWUSEDMEM}
  345. Writeln('Memory used (heapsize): ',DStr(system.Heapsize shr 10),' Kb');
  346. {$endif SHOWUSEDMEM}
  347. {$ifdef fixLeaksOnError}
  348. do_stop{$ifdef FPCPROCVAR}(){$endif};
  349. {$endif fixLeaksOnError}
  350. end;
  351. end.
  352. {
  353. $Log$
  354. Revision 1.39 2003-09-03 11:18:36 florian
  355. * fixed arm concatcopy
  356. + arm support in the common compiler sources added
  357. * moved some generic cg code around
  358. + tfputype added
  359. * ...
  360. Revision 1.38 2003/05/22 21:39:51 peter
  361. * add cgcpu
  362. Revision 1.37 2003/03/23 23:20:38 hajny
  363. + emx target added
  364. Revision 1.36 2003/02/02 19:25:54 carl
  365. * Several bugfixes for m68k target (register alloc., opcode emission)
  366. + VIS target
  367. + Generic add more complete (still not verified)
  368. Revision 1.35 2002/09/05 19:28:31 peter
  369. * removed repetitive pass counting
  370. * display heapsize also for extdebug
  371. Revision 1.34 2002/08/17 09:23:34 florian
  372. * first part of procinfo rewrite
  373. Revision 1.33 2002/07/26 21:15:37 florian
  374. * rewrote the system handling
  375. Revision 1.32 2002/07/11 14:41:27 florian
  376. * start of the new generic parameter handling
  377. Revision 1.31 2002/07/04 19:00:23 florian
  378. + x86_64 define added
  379. Revision 1.30 2002/07/01 18:46:22 peter
  380. * internal linker
  381. * reorganized aasm layer
  382. Revision 1.29 2002/05/18 13:34:06 peter
  383. * readded missing revisions
  384. Revision 1.28 2002/05/16 19:46:35 carl
  385. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  386. + try to fix temp allocation (still in ifdef)
  387. + generic constructor calls
  388. + start of tassembler / tmodulebase class cleanup
  389. Revision 1.26 2002/05/12 16:53:05 peter
  390. * moved entry and exitcode to ncgutil and cgobj
  391. * foreach gets extra argument for passing local data to the
  392. iterator function
  393. * -CR checks also class typecasts at runtime by changing them
  394. into as
  395. * fixed compiler to cycle with the -CR option
  396. * fixed stabs with elf writer, finally the global variables can
  397. be watched
  398. * removed a lot of routines from cga unit and replaced them by
  399. calls to cgobj
  400. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  401. u32bit then the other is typecasted also to u32bit without giving
  402. a rangecheck warning/error.
  403. * fixed pascal calling method with reversing also the high tree in
  404. the parast, detected by tcalcst3 test
  405. Revision 1.25 2002/04/15 19:53:54 peter
  406. * fixed conflicts between the last 2 commits
  407. Revision 1.24 2002/04/15 18:56:42 carl
  408. + InitAsm
  409. Revision 1.23 2002/03/24 19:05:31 carl
  410. + patch for SPARC from Mazen NEIFER
  411. }