compiler.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 MACOS_USE_FAKE_SYSUTILS}
  123. sysutils,
  124. {$ENDIF MACOS_USE_FAKE_SYSUTILS}
  125. verbose,comphook,systems,
  126. cutils,cclasses,globals,options,fmodule,parser,symtable,
  127. assemble,link,dbgbase,import,export,tokens,pass_1
  128. { cpu parameter handling }
  129. ,cpupara
  130. { procinfo stuff }
  131. ,cpupi
  132. { cpu codegenerator }
  133. ,cgcpu
  134. {$ifndef NOPASS2}
  135. ,cpunode
  136. {$endif}
  137. { cpu targets }
  138. ,cputarg
  139. { system information for source system }
  140. { the information about the target os }
  141. { are pulled in by the t_* units }
  142. {$ifdef amiga}
  143. ,i_amiga
  144. {$endif amiga}
  145. {$ifdef atari}
  146. ,i_atari
  147. {$endif atari}
  148. {$ifdef beos}
  149. ,i_beos
  150. {$endif beos}
  151. {$ifdef fbsd}
  152. ,i_fbsd
  153. {$endif fbsd}
  154. {$ifdef gba}
  155. ,i_gba
  156. {$endif gba}
  157. {$ifdef go32v2}
  158. ,i_go32v2
  159. {$endif go32v2}
  160. {$ifdef linux}
  161. ,i_linux
  162. {$endif linux}
  163. {$ifdef macos}
  164. ,i_macos
  165. {$endif macos}
  166. {$ifdef nwm}
  167. ,i_nwm
  168. {$endif nwm}
  169. {$ifdef nwl}
  170. ,i_nwl
  171. {$endif nwm}
  172. {$ifdef os2}
  173. {$ifdef emx}
  174. ,i_emx
  175. {$else emx}
  176. ,i_os2
  177. {$endif emx}
  178. {$endif os2}
  179. {$ifdef palmos}
  180. ,i_palmos
  181. {$endif palmos}
  182. {$ifdef solaris}
  183. ,i_sunos
  184. {$endif solaris}
  185. {$ifdef wdosx}
  186. ,i_wdosx
  187. {$endif wdosx}
  188. {$ifdef win32}
  189. ,i_win
  190. {$endif win32}
  191. ;
  192. function Compile(const cmd:string):longint;
  193. implementation
  194. uses
  195. aasmcpu;
  196. {$ifdef EXTDEBUG}
  197. {$define SHOWUSEDMEM}
  198. {$endif}
  199. {$ifdef MEMDEBUG}
  200. {$define SHOWUSEDMEM}
  201. {$endif}
  202. var
  203. CompilerInitedAfterArgs,
  204. CompilerInited : boolean;
  205. {****************************************************************************
  206. Compiler
  207. ****************************************************************************}
  208. procedure DoneCompiler;
  209. begin
  210. if not CompilerInited then
  211. exit;
  212. { Free compiler if args are read }
  213. {$ifdef BrowserLog}
  214. DoneBrowserLog;
  215. {$endif BrowserLog}
  216. {$ifdef BrowserCol}
  217. do_doneSymbolInfo;
  218. {$endif BrowserCol}
  219. if CompilerInitedAfterArgs then
  220. begin
  221. CompilerInitedAfterArgs:=false;
  222. DoneParser;
  223. DoneImport;
  224. DoneExport;
  225. DoneDebuginfo;
  226. DoneLinker;
  227. DoneAssembler;
  228. DoneAsm;
  229. end;
  230. { Free memory for the others }
  231. CompilerInited:=false;
  232. DoneSymtable;
  233. DoneGlobals;
  234. donetokens;
  235. end;
  236. procedure InitCompiler(const cmd:string);
  237. begin
  238. if CompilerInited then
  239. DoneCompiler;
  240. { inits which need to be done before the arguments are parsed }
  241. InitSystems;
  242. { globals depends on source_info so it must be after systems }
  243. InitGlobals;
  244. { verbose depends on exe_path and must be after globals }
  245. InitVerbose;
  246. {$ifdef BrowserLog}
  247. InitBrowserLog;
  248. {$endif BrowserLog}
  249. {$ifdef BrowserCol}
  250. do_initSymbolInfo;
  251. {$endif BrowserCol}
  252. inittokens;
  253. InitSymtable; {Must come before read_arguments, to enable macrosymstack}
  254. CompilerInited:=true;
  255. { this is needed here for the IDE
  256. in case of compilation failure
  257. at the previous compile }
  258. current_module:=nil;
  259. { read the arguments }
  260. read_arguments(cmd);
  261. { inits which depend on arguments }
  262. InitParser;
  263. InitImport;
  264. InitExport;
  265. InitLinker;
  266. InitAssembler;
  267. InitDebugInfo;
  268. InitAsm;
  269. CompilerInitedAfterArgs:=true;
  270. end;
  271. function Compile(const cmd:string):longint;
  272. {$ifdef fpc}
  273. {$maxfpuregisters 0}
  274. {$endif fpc}
  275. procedure writepathlist(w:longint;l:TSearchPathList);
  276. var
  277. hp : tstringlistitem;
  278. begin
  279. hp:=tstringlistitem(l.first);
  280. while assigned(hp) do
  281. begin
  282. Message1(w,hp.str);
  283. hp:=tstringlistitem(hp.next);
  284. end;
  285. end;
  286. function getrealtime : real;
  287. var
  288. {$IFDEF USE_SYSUTILS}
  289. h,m,s,s1000 : word;
  290. {$ELSE USE_SYSUTILS}
  291. h,m,s,s100 : word;
  292. {$ENDIF USE_SYSUTILS}
  293. begin
  294. {$IFDEF USE_SYSUTILS}
  295. DecodeTime(Time,h,m,s,s1000);
  296. getrealtime:=h*3600.0+m*60.0+s+s1000/1000.0;
  297. {$ELSE USE_SYSUTILS}
  298. gettime(h,m,s,s100);
  299. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  300. {$ENDIF USE_SYSUTILS}
  301. end;
  302. var
  303. starttime : real;
  304. timestr : string[20];
  305. linkstr : string[64];
  306. {$ifdef SHOWUSEDMEM}
  307. hstatus : TFPCHeapStatus;
  308. {$endif SHOWUSEDMEM}
  309. begin
  310. try
  311. try
  312. { Initialize the compiler }
  313. InitCompiler(cmd);
  314. { show some info }
  315. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  316. Message1(general_d_sourceos,source_info.name);
  317. Message1(general_i_targetos,target_info.name);
  318. Message1(general_t_exepath,exepath);
  319. WritePathList(general_t_unitpath,unitsearchpath);
  320. WritePathList(general_t_includepath,includesearchpath);
  321. WritePathList(general_t_librarypath,librarysearchpath);
  322. WritePathList(general_t_objectpath,objectsearchpath);
  323. starttime:=getrealtime;
  324. { Compile the program }
  325. {$ifdef PREPROCWRITE}
  326. if parapreprocess then
  327. parser.preprocess(inputdir+inputfile+inputextension)
  328. else
  329. {$endif PREPROCWRITE}
  330. parser.compile(inputdir+inputfile+inputextension);
  331. { Show statistics }
  332. if status.errorcount=0 then
  333. begin
  334. starttime:=getrealtime-starttime;
  335. if starttime<0 then
  336. starttime:=starttime+3600.0*24.0;
  337. timestr:=tostr(trunc(starttime))+'.'+tostr(trunc(frac(starttime)*10));
  338. if status.codesize<>-1 then
  339. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  340. else
  341. linkstr:='';
  342. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  343. if (Status.Verbosity and V_Warning = V_Warning) and
  344. (Status.CountWarnings <> 0) then
  345. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  346. if (Status.Verbosity and V_Hint = V_Hint) and
  347. (Status.CountHints <> 0) then
  348. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  349. if (Status.Verbosity and V_Note = V_Note) and
  350. (Status.CountNotes <> 0) then
  351. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  352. end;
  353. finally
  354. { no message possible after this !! }
  355. DoneCompiler;
  356. end;
  357. DoneVerbose;
  358. except
  359. on EControlCAbort do
  360. begin
  361. try
  362. { in case of 50 errors, this could cause another exception,
  363. suppress this exception
  364. }
  365. Message(general_f_compilation_aborted);
  366. except
  367. on ECompilerAbort do
  368. ;
  369. end;
  370. DoneVerbose;
  371. end;
  372. on ECompilerAbort do
  373. begin
  374. try
  375. { in case of 50 errors, this could cause another exception,
  376. suppress this exception
  377. }
  378. Message(general_f_compilation_aborted);
  379. except
  380. on ECompilerAbort do
  381. ;
  382. end;
  383. DoneVerbose;
  384. end;
  385. on ECompilerAbortSilent do
  386. begin
  387. DoneVerbose;
  388. end;
  389. on Exception do
  390. begin
  391. { General catchall, normally not used }
  392. try
  393. { in case of 50 errors, this could cause another exception,
  394. suppress this exception
  395. }
  396. Message(general_f_compilation_aborted);
  397. except
  398. on ECompilerAbort do
  399. ;
  400. end;
  401. DoneVerbose;
  402. Raise;
  403. end;
  404. end;
  405. {$ifdef SHOWUSEDMEM}
  406. hstatus:=GetFPCHeapStatus;
  407. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  408. {$endif SHOWUSEDMEM}
  409. { Set the return value if an error has occurred }
  410. if status.errorcount=0 then
  411. result:=0
  412. else
  413. result:=1;
  414. end;
  415. end.