compiler.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. {$ifdef WATCOM} // wiktor: pewnie nie potrzeba
  98. emu387,
  99. { dpmiexcp, }
  100. {$endif WATCOM}
  101. {$endif}
  102. {$ifdef USEEXCEPT}
  103. tpexcept,
  104. {$endif USEEXCEPT}
  105. {$ifdef BrowserLog}
  106. browlog,
  107. {$endif BrowserLog}
  108. {$ifdef Delphi}
  109. dmisc,
  110. {$else Delphi}
  111. dos,
  112. {$endif Delphi}
  113. verbose,comphook,systems,
  114. cutils,cclasses,globals,options,fmodule,parser,symtable,
  115. assemble,link,import,export,tokens,pass_1
  116. { cpu overrides }
  117. ,cpuswtch
  118. { cpu codegenerator }
  119. ,cgcpu
  120. {$ifndef NOPASS2}
  121. ,cpunode
  122. {$endif}
  123. { cpu targets }
  124. ,cputarg
  125. { cpu parameter handling }
  126. ,cpupara
  127. { procinfo stuff }
  128. ,cpupi
  129. { system information for source system }
  130. { the information about the target os }
  131. { are pulled in by the t_* units }
  132. {$ifdef amiga}
  133. ,i_amiga
  134. {$endif amiga}
  135. {$ifdef atari}
  136. ,i_atari
  137. {$endif atari}
  138. {$ifdef beos}
  139. ,i_beos
  140. {$endif beos}
  141. {$ifdef fbds}
  142. ,i_fbsd
  143. {$endif fbds}
  144. {$ifdef go32v2}
  145. ,i_go32v2
  146. {$endif go32v2}
  147. {$ifdef linux}
  148. ,i_linux
  149. {$endif linux}
  150. {$ifdef macos}
  151. ,i_macos
  152. {$endif macos}
  153. {$ifdef nwm}
  154. ,i_nwm
  155. {$endif nwm}
  156. {$ifdef os2}
  157. {$ifdef emx}
  158. ,i_emx
  159. {$else emx}
  160. ,i_os2
  161. {$endif emx}
  162. {$endif os2}
  163. {$ifdef palmos}
  164. ,i_palmos
  165. {$endif palmos}
  166. {$ifdef sunos}
  167. ,i_sunos
  168. {$endif sunos}
  169. {$ifdef wdosx}
  170. ,i_wdosx
  171. {$endif wdosx}
  172. {$ifdef win32}
  173. ,i_win32
  174. {$endif win32}
  175. { assembler readers }
  176. {$ifdef i386}
  177. {$ifndef NoRa386Int}
  178. ,ra386int
  179. {$endif NoRa386Int}
  180. {$ifndef NoRa386Att}
  181. ,ra386att
  182. {$endif NoRa386Att}
  183. {$else}
  184. ,rasm
  185. {$endif i386}
  186. {$ifdef powerpc}
  187. ,rappcgas
  188. {$endif powerpc}
  189. {$ifdef arm}
  190. ,raarmgas
  191. {$endif arm}
  192. {$ifdef SPARC}
  193. ,racpugas
  194. {$endif SPARC}
  195. ;
  196. function Compile(const cmd:string):longint;
  197. implementation
  198. uses
  199. aasmcpu;
  200. {$ifdef EXTDEBUG}
  201. {$define SHOWUSEDMEM}
  202. {$endif}
  203. {$ifdef MEMDEBUG}
  204. {$define SHOWUSEDMEM}
  205. {$endif}
  206. var
  207. CompilerInitedAfterArgs,
  208. CompilerInited : boolean;
  209. olddo_stop : tstopprocedure;
  210. {$ifdef USEEXCEPT}
  211. procedure RecoverStop;
  212. begin
  213. if recoverpospointer<>nil then
  214. LongJmp(recoverpospointer^,1)
  215. else
  216. Do_Halt(1);
  217. end;
  218. {$endif USEEXCEPT}
  219. {****************************************************************************
  220. Compiler
  221. ****************************************************************************}
  222. procedure DoneCompiler;
  223. begin
  224. if not CompilerInited then
  225. exit;
  226. { Free compiler if args are read }
  227. {$ifdef BrowserLog}
  228. DoneBrowserLog;
  229. {$endif BrowserLog}
  230. {$ifdef BrowserCol}
  231. do_doneSymbolInfo;
  232. {$endif BrowserCol}
  233. if CompilerInitedAfterArgs then
  234. begin
  235. CompilerInitedAfterArgs:=false;
  236. DoneParser;
  237. DoneImport;
  238. DoneExport;
  239. DoneLinker;
  240. DoneAssembler;
  241. DoneAsm;
  242. end;
  243. { Free memory for the others }
  244. CompilerInited:=false;
  245. DoneSymtable;
  246. DoneGlobals;
  247. donetokens;
  248. {$ifdef USEEXCEPT}
  249. recoverpospointer:=nil;
  250. longjump_used:=false;
  251. {$endif USEEXCEPT}
  252. end;
  253. procedure InitCompiler(const cmd:string);
  254. begin
  255. if CompilerInited then
  256. DoneCompiler;
  257. { inits which need to be done before the arguments are parsed }
  258. InitSystems;
  259. { globals depends on source_info so it must be after systems }
  260. InitGlobals;
  261. { verbose depends on exe_path and must be after globals }
  262. InitVerbose;
  263. {$ifdef BrowserLog}
  264. InitBrowserLog;
  265. {$endif BrowserLog}
  266. {$ifdef BrowserCol}
  267. do_initSymbolInfo;
  268. {$endif BrowserCol}
  269. inittokens;
  270. InitSymtable;
  271. CompilerInited:=true;
  272. { this is needed here for the IDE
  273. in case of compilation failure
  274. at the previous compile }
  275. current_module:=nil;
  276. { read the arguments }
  277. read_arguments(cmd);
  278. { inits which depend on arguments }
  279. InitParser;
  280. InitImport;
  281. InitExport;
  282. InitLinker;
  283. InitAssembler;
  284. InitAsm;
  285. CompilerInitedAfterArgs:=true;
  286. end;
  287. procedure minimal_stop;
  288. begin
  289. DoneCompiler;
  290. olddo_stop{$ifdef FPCPROCVAR}(){$endif};
  291. end;
  292. function Compile(const cmd:string):longint;
  293. {$ifdef fpc}
  294. {$maxfpuregisters 0}
  295. {$endif fpc}
  296. procedure writepathlist(w:longint;l:TSearchPathList);
  297. var
  298. hp : tstringlistitem;
  299. begin
  300. hp:=tstringlistitem(l.first);
  301. while assigned(hp) do
  302. begin
  303. Message1(w,hp.str);
  304. hp:=tstringlistitem(hp.next);
  305. end;
  306. end;
  307. function getrealtime : real;
  308. var
  309. h,m,s,s100 : word;
  310. begin
  311. gettime(h,m,s,s100);
  312. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  313. end;
  314. var
  315. starttime : real;
  316. {$ifdef USEEXCEPT}
  317. recoverpos : jmp_buf;
  318. {$endif}
  319. begin
  320. olddo_stop:=do_stop;
  321. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  322. { Initialize the compiler }
  323. InitCompiler(cmd);
  324. { show some info }
  325. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  326. Message1(general_d_sourceos,source_info.name);
  327. Message1(general_i_targetos,target_info.name);
  328. Message1(general_t_exepath,exepath);
  329. WritePathList(general_t_unitpath,unitsearchpath);
  330. WritePathList(general_t_includepath,includesearchpath);
  331. WritePathList(general_t_librarypath,librarysearchpath);
  332. WritePathList(general_t_objectpath,objectsearchpath);
  333. {$ifdef USEEXCEPT}
  334. if setjmp(recoverpos)=0 then
  335. begin
  336. recoverpospointer:=@recoverpos;
  337. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  338. {$endif USEEXCEPT}
  339. starttime:=getrealtime;
  340. {$ifdef PREPROCWRITE}
  341. if parapreprocess then
  342. parser.preprocess(inputdir+inputfile+inputextension)
  343. else
  344. {$endif PREPROCWRITE}
  345. parser.compile(inputdir+inputfile+inputextension);
  346. if status.errorcount=0 then
  347. begin
  348. starttime:=getrealtime-starttime;
  349. if starttime<0 then
  350. starttime:=starttime+3600.0*24.0;
  351. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  352. '.'+tostr(trunc(frac(starttime)*10)));
  353. end;
  354. {$ifdef USEEXCEPT}
  355. end;
  356. {$endif USEEXCEPT}
  357. { Stop is always called, so we come here when a program is compiled or not }
  358. do_stop:=olddo_stop;
  359. { Stop the compiler, frees also memory }
  360. { no message possible after this !! }
  361. DoneCompiler;
  362. { Set the return value if an error has occurred }
  363. if status.errorcount=0 then
  364. Compile:=0
  365. else
  366. Compile:=1;
  367. DoneVerbose;
  368. {$ifdef SHOWUSEDMEM}
  369. Writeln('Memory used (heapsize): ',DStr(system.Heapsize shr 10),' Kb');
  370. {$endif SHOWUSEDMEM}
  371. {$ifdef fixLeaksOnError}
  372. do_stop{$ifdef FPCPROCVAR}(){$endif};
  373. {$endif fixLeaksOnError}
  374. end;
  375. end.
  376. {
  377. $Log$
  378. Revision 1.43 2003-12-04 10:46:19 mazen
  379. + added support for spac assembler reader
  380. Revision 1.42 2003/11/17 23:23:47 florian
  381. + first part of arm assembler reader
  382. Revision 1.41 2003/11/12 16:05:39 florian
  383. * assembler readers OOPed
  384. + typed currency constants
  385. + typed 128 bit float constants if the CPU supports it
  386. Revision 1.40 2003/09/05 17:41:12 florian
  387. * merged Wiktor's Watcom patches in 1.1
  388. Revision 1.39 2003/09/03 11:18:36 florian
  389. * fixed arm concatcopy
  390. + arm support in the common compiler sources added
  391. * moved some generic cg code around
  392. + tfputype added
  393. * ...
  394. Revision 1.38 2003/05/22 21:39:51 peter
  395. * add cgcpu
  396. Revision 1.37 2003/03/23 23:20:38 hajny
  397. + emx target added
  398. Revision 1.36 2003/02/02 19:25:54 carl
  399. * Several bugfixes for m68k target (register alloc., opcode emission)
  400. + VIS target
  401. + Generic add more complete (still not verified)
  402. Revision 1.35 2002/09/05 19:28:31 peter
  403. * removed repetitive pass counting
  404. * display heapsize also for extdebug
  405. Revision 1.34 2002/08/17 09:23:34 florian
  406. * first part of procinfo rewrite
  407. Revision 1.33 2002/07/26 21:15:37 florian
  408. * rewrote the system handling
  409. Revision 1.32 2002/07/11 14:41:27 florian
  410. * start of the new generic parameter handling
  411. Revision 1.31 2002/07/04 19:00:23 florian
  412. + x86_64 define added
  413. Revision 1.30 2002/07/01 18:46:22 peter
  414. * internal linker
  415. * reorganized aasm layer
  416. Revision 1.29 2002/05/18 13:34:06 peter
  417. * readded missing revisions
  418. Revision 1.28 2002/05/16 19:46:35 carl
  419. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  420. + try to fix temp allocation (still in ifdef)
  421. + generic constructor calls
  422. + start of tassembler / tmodulebase class cleanup
  423. Revision 1.26 2002/05/12 16:53:05 peter
  424. * moved entry and exitcode to ncgutil and cgobj
  425. * foreach gets extra argument for passing local data to the
  426. iterator function
  427. * -CR checks also class typecasts at runtime by changing them
  428. into as
  429. * fixed compiler to cycle with the -CR option
  430. * fixed stabs with elf writer, finally the global variables can
  431. be watched
  432. * removed a lot of routines from cga unit and replaced them by
  433. calls to cgobj
  434. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  435. u32bit then the other is typecasted also to u32bit without giving
  436. a rangecheck warning/error.
  437. * fixed pascal calling method with reversing also the high tree in
  438. the parast, detected by tcalcst3 test
  439. Revision 1.25 2002/04/15 19:53:54 peter
  440. * fixed conflicts between the last 2 commits
  441. Revision 1.24 2002/04/15 18:56:42 carl
  442. + InitAsm
  443. Revision 1.23 2002/03/24 19:05:31 carl
  444. + patch for SPARC from Mazen NEIFER
  445. }