compiler.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. {
  2. This unit is the interface of the compiler which can be used by
  3. external programs to link in the compiler
  4. Copyright (c) 1998-2005 by Florian Klaempfl
  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. unit compiler;
  18. {$i fpcdefs.inc}
  19. {$ifdef FPC}
  20. { One of Alpha, I386 or M68K must be defined }
  21. {$UNDEF CPUOK}
  22. {$ifdef I386}
  23. {$define CPUOK}
  24. {$endif}
  25. {$ifdef M68K}
  26. {$ifndef CPUOK}
  27. {$DEFINE CPUOK}
  28. {$else}
  29. {$fatal cannot define two CPU switches}
  30. {$endif}
  31. {$endif}
  32. {$ifdef alpha}
  33. {$ifndef CPUOK}
  34. {$DEFINE CPUOK}
  35. {$else}
  36. {$fatal cannot define two CPU switches}
  37. {$endif}
  38. {$endif}
  39. {$ifdef vis}
  40. {$ifndef CPUOK}
  41. {$DEFINE CPUOK}
  42. {$else}
  43. {$fatal cannot define two CPU switches}
  44. {$endif}
  45. {$endif}
  46. {$ifdef powerpc}
  47. {$ifndef CPUOK}
  48. {$DEFINE CPUOK}
  49. {$else}
  50. {$fatal cannot define two CPU switches}
  51. {$endif}
  52. {$endif}
  53. {$ifdef POWERPC64}
  54. {$ifndef CPUOK}
  55. {$DEFINE CPUOK}
  56. {$else}
  57. {$fatal cannot define two CPU switches}
  58. {$endif}
  59. {$endif}
  60. {$ifdef ia64}
  61. {$ifndef CPUOK}
  62. {$DEFINE CPUOK}
  63. {$else}
  64. {$fatal cannot define two CPU switches}
  65. {$endif}
  66. {$endif}
  67. {$ifdef SPARC}
  68. {$ifndef CPUOK}
  69. {$DEFINE CPUOK}
  70. {$else}
  71. {$fatal cannot define two CPU switches}
  72. {$endif}
  73. {$endif}
  74. {$ifdef x86_64}
  75. {$ifndef CPUOK}
  76. {$DEFINE CPUOK}
  77. {$else}
  78. {$fatal cannot define two CPU switches}
  79. {$endif}
  80. {$endif}
  81. {$ifdef ARM}
  82. {$ifndef CPUOK}
  83. {$DEFINE CPUOK}
  84. {$else}
  85. {$fatal cannot define two CPU switches}
  86. {$endif ARM}
  87. {$endif ARM}
  88. {$ifdef MIPS}
  89. {$ifndef CPUOK}
  90. {$DEFINE CPUOK}
  91. {$else}
  92. {$fatal cannot define two CPU switches}
  93. {$endif MIPS}
  94. {$endif MIPS}
  95. {$ifndef CPUOK}
  96. {$fatal One of the switches I386, iA64, Alpha, PowerPC or M68K must be defined}
  97. {$endif}
  98. {$ifdef support_mmx}
  99. {$ifndef i386}
  100. {$fatal I386 switch must be on for MMX support}
  101. {$endif i386}
  102. {$endif support_mmx}
  103. {$endif}
  104. interface
  105. uses
  106. {$ifdef fpc}
  107. {$ifdef GO32V2}
  108. emu387,
  109. {$endif GO32V2}
  110. {$ifdef WATCOM} // wiktor: pewnie nie potrzeba
  111. emu387,
  112. { dpmiexcp, }
  113. {$endif WATCOM}
  114. {$endif}
  115. {$ifdef BrowserLog}
  116. browlog,
  117. {$endif BrowserLog}
  118. {$IFDEF USE_SYSUTILS}
  119. {$ELSE USE_SYSUTILS}
  120. dos,
  121. {$ENDIF USE_SYSUTILS}
  122. {$IFNDEF USE_FAKE_SYSUTILS}
  123. sysutils,
  124. {$ELSE}
  125. fksysutl,
  126. {$ENDIF}
  127. verbose,comphook,systems,
  128. cutils,cclasses,globals,options,fmodule,parser,symtable,
  129. assemble,link,dbgbase,import,export,tokens,pass_1
  130. { cpu parameter handling }
  131. ,cpupara
  132. { procinfo stuff }
  133. ,cpupi
  134. { cpu codegenerator }
  135. ,cgcpu
  136. {$ifndef NOPASS2}
  137. ,cpunode
  138. {$endif}
  139. { cpu targets }
  140. ,cputarg
  141. { system information for source system }
  142. { the information about the target os }
  143. { are pulled in by the t_* units }
  144. {$ifdef amiga}
  145. ,i_amiga
  146. {$endif amiga}
  147. {$ifdef atari}
  148. ,i_atari
  149. {$endif atari}
  150. {$ifdef beos}
  151. ,i_beos
  152. {$endif beos}
  153. {$ifdef fbsd}
  154. ,i_fbsd
  155. {$endif fbsd}
  156. {$ifdef gba}
  157. ,i_gba
  158. {$endif gba}
  159. {$ifdef go32v2}
  160. ,i_go32v2
  161. {$endif go32v2}
  162. {$ifdef linux}
  163. ,i_linux
  164. {$endif linux}
  165. {$ifdef macos}
  166. ,i_macos
  167. {$endif macos}
  168. {$ifdef nwm}
  169. ,i_nwm
  170. {$endif nwm}
  171. {$ifdef nwl}
  172. ,i_nwl
  173. {$endif nwm}
  174. {$ifdef os2}
  175. {$ifdef emx}
  176. ,i_emx
  177. {$else emx}
  178. ,i_os2
  179. {$endif emx}
  180. {$endif os2}
  181. {$ifdef palmos}
  182. ,i_palmos
  183. {$endif palmos}
  184. {$ifdef solaris}
  185. ,i_sunos
  186. {$endif solaris}
  187. {$ifdef wdosx}
  188. ,i_wdosx
  189. {$endif wdosx}
  190. {$ifdef win32}
  191. ,i_win
  192. {$endif win32}
  193. ;
  194. function Compile(const cmd:string):longint;
  195. implementation
  196. uses
  197. aasmcpu;
  198. {$ifdef EXTDEBUG}
  199. {$define SHOWUSEDMEM}
  200. {$endif}
  201. {$ifdef MEMDEBUG}
  202. {$define SHOWUSEDMEM}
  203. {$endif}
  204. var
  205. CompilerInitedAfterArgs,
  206. CompilerInited : boolean;
  207. {****************************************************************************
  208. Compiler
  209. ****************************************************************************}
  210. procedure DoneCompiler;
  211. begin
  212. if not CompilerInited then
  213. exit;
  214. { Free compiler if args are read }
  215. {$ifdef BrowserLog}
  216. DoneBrowserLog;
  217. {$endif BrowserLog}
  218. {$ifdef BrowserCol}
  219. do_doneSymbolInfo;
  220. {$endif BrowserCol}
  221. if CompilerInitedAfterArgs then
  222. begin
  223. CompilerInitedAfterArgs:=false;
  224. DoneParser;
  225. DoneImport;
  226. DoneExport;
  227. DoneDebuginfo;
  228. DoneLinker;
  229. DoneAssembler;
  230. DoneAsm;
  231. end;
  232. { Free memory for the others }
  233. CompilerInited:=false;
  234. DoneSymtable;
  235. DoneGlobals;
  236. donetokens;
  237. end;
  238. procedure InitCompiler(const cmd:string);
  239. begin
  240. if CompilerInited then
  241. DoneCompiler;
  242. { inits which need to be done before the arguments are parsed }
  243. InitSystems;
  244. { globals depends on source_info so it must be after systems }
  245. InitGlobals;
  246. { verbose depends on exe_path and must be after globals }
  247. InitVerbose;
  248. {$ifdef BrowserLog}
  249. InitBrowserLog;
  250. {$endif BrowserLog}
  251. {$ifdef BrowserCol}
  252. do_initSymbolInfo;
  253. {$endif BrowserCol}
  254. inittokens;
  255. InitSymtable; {Must come before read_arguments, to enable macrosymstack}
  256. CompilerInited:=true;
  257. { this is needed here for the IDE
  258. in case of compilation failure
  259. at the previous compile }
  260. current_module:=nil;
  261. { read the arguments }
  262. read_arguments(cmd);
  263. { inits which depend on arguments }
  264. InitParser;
  265. InitImport;
  266. InitExport;
  267. InitLinker;
  268. InitAssembler;
  269. InitDebugInfo;
  270. InitAsm;
  271. CompilerInitedAfterArgs:=true;
  272. end;
  273. function Compile(const cmd:string):longint;
  274. {$ifdef fpc}
  275. {$maxfpuregisters 0}
  276. {$endif fpc}
  277. procedure writepathlist(w:longint;l:TSearchPathList);
  278. var
  279. hp : tstringlistitem;
  280. begin
  281. hp:=tstringlistitem(l.first);
  282. while assigned(hp) do
  283. begin
  284. Message1(w,hp.str);
  285. hp:=tstringlistitem(hp.next);
  286. end;
  287. end;
  288. function getrealtime : real;
  289. var
  290. {$IFDEF USE_SYSUTILS}
  291. h,m,s,s1000 : word;
  292. {$ELSE USE_SYSUTILS}
  293. h,m,s,s100 : word;
  294. {$ENDIF USE_SYSUTILS}
  295. begin
  296. {$IFDEF USE_SYSUTILS}
  297. DecodeTime(Time,h,m,s,s1000);
  298. getrealtime:=h*3600.0+m*60.0+s+s1000/1000.0;
  299. {$ELSE USE_SYSUTILS}
  300. gettime(h,m,s,s100);
  301. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  302. {$ENDIF USE_SYSUTILS}
  303. end;
  304. var
  305. starttime : real;
  306. timestr : string[20];
  307. linkstr : string[64];
  308. {$ifdef SHOWUSEDMEM}
  309. hstatus : TFPCHeapStatus;
  310. {$endif SHOWUSEDMEM}
  311. begin
  312. try
  313. try
  314. { Initialize the compiler }
  315. InitCompiler(cmd);
  316. { show some info }
  317. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  318. Message1(general_d_sourceos,source_info.name);
  319. Message1(general_i_targetos,target_info.name);
  320. Message1(general_t_exepath,exepath);
  321. WritePathList(general_t_unitpath,unitsearchpath);
  322. WritePathList(general_t_includepath,includesearchpath);
  323. WritePathList(general_t_librarypath,librarysearchpath);
  324. WritePathList(general_t_objectpath,objectsearchpath);
  325. starttime:=getrealtime;
  326. { Compile the program }
  327. {$ifdef PREPROCWRITE}
  328. if parapreprocess then
  329. parser.preprocess(inputdir+inputfile+inputextension)
  330. else
  331. {$endif PREPROCWRITE}
  332. parser.compile(inputdir+inputfile+inputextension);
  333. { Show statistics }
  334. if status.errorcount=0 then
  335. begin
  336. starttime:=getrealtime-starttime;
  337. if starttime<0 then
  338. starttime:=starttime+3600.0*24.0;
  339. timestr:=tostr(trunc(starttime))+'.'+tostr(trunc(frac(starttime)*10));
  340. if status.codesize<>-1 then
  341. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  342. else
  343. linkstr:='';
  344. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  345. if (Status.Verbosity and V_Warning = V_Warning) and
  346. (Status.CountWarnings <> 0) then
  347. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  348. if (Status.Verbosity and V_Hint = V_Hint) and
  349. (Status.CountHints <> 0) then
  350. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  351. if (Status.Verbosity and V_Note = V_Note) and
  352. (Status.CountNotes <> 0) then
  353. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  354. end;
  355. finally
  356. { no message possible after this !! }
  357. DoneCompiler;
  358. end;
  359. DoneVerbose;
  360. except
  361. on EControlCAbort do
  362. begin
  363. try
  364. { in case of 50 errors, this could cause another exception,
  365. suppress this exception
  366. }
  367. Message(general_f_compilation_aborted);
  368. except
  369. on ECompilerAbort do
  370. ;
  371. end;
  372. DoneVerbose;
  373. end;
  374. on ECompilerAbort do
  375. begin
  376. try
  377. { in case of 50 errors, this could cause another exception,
  378. suppress this exception
  379. }
  380. Message(general_f_compilation_aborted);
  381. except
  382. on ECompilerAbort do
  383. ;
  384. end;
  385. DoneVerbose;
  386. end;
  387. on ECompilerAbortSilent do
  388. begin
  389. DoneVerbose;
  390. end;
  391. on Exception do
  392. begin
  393. { General catchall, normally not used }
  394. try
  395. { in case of 50 errors, this could cause another exception,
  396. suppress this exception
  397. }
  398. Message(general_f_compilation_aborted);
  399. except
  400. on ECompilerAbort do
  401. ;
  402. end;
  403. DoneVerbose;
  404. Raise;
  405. end;
  406. end;
  407. {$ifdef SHOWUSEDMEM}
  408. hstatus:=GetFPCHeapStatus;
  409. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  410. {$endif SHOWUSEDMEM}
  411. { Set the return value if an error has occurred }
  412. if status.errorcount=0 then
  413. result:=0
  414. else
  415. result:=1;
  416. end;
  417. end.