2
0

compiler.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. interface
  20. uses
  21. {$ifdef GO32V2}
  22. emu387,
  23. {$endif GO32V2}
  24. {$ifdef WATCOM}
  25. emu387,
  26. {$endif WATCOM}
  27. {$if defined(unix) and (FPC_FULLVERSION>20700)}
  28. { system code page stuff for unix }
  29. unixcp,
  30. {$endif}
  31. {$IFNDEF USE_FAKE_SYSUTILS}
  32. sysutils,math,
  33. {$ELSE}
  34. fksysutl,
  35. {$ENDIF}
  36. verbose,comphook,systems,
  37. cutils,cfileutl,cclasses,globals,options,fmodule,parser,symtable,
  38. assemble,link,dbgbase,import,export,tokens,pass_1,wpobase,wpo
  39. { cpu parameter handling }
  40. ,cpupara
  41. { procinfo stuff }
  42. ,cpupi
  43. { cpu codegenerator }
  44. ,cgcpu
  45. {$ifndef NOPASS2}
  46. ,cpunode
  47. {$endif}
  48. { cpu targets }
  49. ,cputarg
  50. {$ifdef llvm}
  51. ,llvmtarg
  52. {$endif llvm}
  53. { system information for source system }
  54. { the information about the target os }
  55. { are pulled in by the t_* units }
  56. {$ifdef amiga}
  57. ,i_amiga
  58. {$endif amiga}
  59. {$ifdef android}
  60. ,i_android
  61. {$endif android}
  62. {$ifdef aros}
  63. ,i_aros
  64. {$endif}
  65. {$ifdef atari}
  66. ,i_atari
  67. {$endif atari}
  68. {$ifdef beos}
  69. ,i_beos
  70. {$endif beos}
  71. {$ifdef fbsd}
  72. ,i_fbsd
  73. {$endif fbsd}
  74. {$ifdef gba}
  75. ,i_gba
  76. {$endif gba}
  77. {$ifdef go32v2}
  78. ,i_go32v2
  79. {$endif go32v2}
  80. {$ifdef linux}
  81. ,i_linux
  82. {$endif linux}
  83. {$ifdef macos}
  84. ,i_macos
  85. {$endif macos}
  86. {$ifdef nds}
  87. ,i_nds
  88. {$endif nds}
  89. {$ifdef nwm}
  90. ,i_nwm
  91. {$endif nwm}
  92. {$ifdef nwl}
  93. ,i_nwl
  94. {$endif nwm}
  95. {$ifdef os2}
  96. {$ifdef emx}
  97. ,i_emx
  98. {$else emx}
  99. ,i_os2
  100. {$endif emx}
  101. {$endif os2}
  102. {$ifdef palmos}
  103. ,i_palmos
  104. {$endif palmos}
  105. {$ifdef solaris}
  106. ,i_sunos
  107. {$endif solaris}
  108. {$ifdef wdosx}
  109. ,i_wdosx
  110. {$endif wdosx}
  111. {$ifdef wii}
  112. ,i_wii
  113. {$endif wii}
  114. {$ifdef win32}
  115. ,i_win
  116. {$endif win32}
  117. {$ifdef symbian}
  118. ,i_symbian
  119. {$endif symbian}
  120. {$ifdef nativent}
  121. ,i_nativent
  122. {$endif nativent}
  123. {$ifdef aix}
  124. ,i_aix
  125. {$endif aix}
  126. ,globtype;
  127. function Compile(const cmd:TCmdStr):longint;
  128. implementation
  129. uses
  130. aasmcpu;
  131. {$if defined(EXTDEBUG) or defined(MEMDEBUG)}
  132. {$define SHOWUSEDMEM}
  133. {$endif}
  134. var
  135. CompilerInitedAfterArgs,
  136. CompilerInited : boolean;
  137. {****************************************************************************
  138. Compiler
  139. ****************************************************************************}
  140. procedure DoneCompiler;
  141. begin
  142. if not CompilerInited then
  143. exit;
  144. { Free compiler if args are read }
  145. if CompilerInitedAfterArgs then
  146. begin
  147. CompilerInitedAfterArgs:=false;
  148. DoneParser;
  149. DoneImport;
  150. DoneExport;
  151. DoneLinker;
  152. DoneAsm;
  153. DoneWpo;
  154. end;
  155. { Free memory for the others }
  156. CompilerInited:=false;
  157. do_doneSymbolInfo;
  158. DoneSymtable;
  159. DoneGlobals;
  160. DoneFileUtils;
  161. donetokens;
  162. end;
  163. procedure InitCompiler(const cmd:TCmdStr);
  164. begin
  165. if CompilerInited then
  166. DoneCompiler;
  167. {$if defined(unix) and (FPC_FULLVERSION>20700)}
  168. { Set default code page for ansistrings on unix-like systems }
  169. DefaultSystemCodePage:=GetSystemCodePage;
  170. {$endif}
  171. { inits which need to be done before the arguments are parsed }
  172. InitSystems;
  173. { fileutils depends on source_info so it must be after systems }
  174. InitFileUtils;
  175. { globals depends on source_info so it must be after systems }
  176. InitGlobals;
  177. { verbose depends on exe_path and must be after globals }
  178. InitVerbose;
  179. inittokens;
  180. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  181. do_initSymbolInfo;
  182. CompilerInited:=true;
  183. { this is needed here for the IDE
  184. in case of compilation failure
  185. at the previous compile }
  186. set_current_module(nil);
  187. { read the arguments }
  188. read_arguments(cmd);
  189. { inits which depend on arguments }
  190. InitParser;
  191. InitImport;
  192. InitExport;
  193. InitLinker;
  194. InitAsm;
  195. InitWpo;
  196. CompilerInitedAfterArgs:=true;
  197. end;
  198. function Compile(const cmd:TCmdStr):longint;
  199. {$maxfpuregisters 0}
  200. procedure writepathlist(w:longint;l:TSearchPathList);
  201. var
  202. hp : TCmdStrListItem;
  203. begin
  204. hp:=TCmdStrListItem(l.first);
  205. while assigned(hp) do
  206. begin
  207. Message1(w,hp.str);
  208. hp:=TCmdStrListItem(hp.next);
  209. end;
  210. end;
  211. var
  212. timestr : string[20];
  213. linkstr : string[64];
  214. {$ifdef SHOWUSEDMEM}
  215. hstatus : TFPCHeapStatus;
  216. {$endif SHOWUSEDMEM}
  217. ExceptionMask : TFPUExceptionMask;
  218. totaltime : real;
  219. begin
  220. try
  221. try
  222. ExceptionMask:=GetExceptionMask;
  223. SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
  224. exOverflow, exUnderflow, exPrecision]);
  225. starttime:=getrealtime;
  226. { Initialize the compiler }
  227. InitCompiler(cmd);
  228. { show some info }
  229. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  230. Message1(general_d_sourceos,source_info.name);
  231. Message1(general_i_targetos,target_info.name);
  232. Message1(general_t_exepath,exepath);
  233. WritePathList(general_t_unitpath,unitsearchpath);
  234. WritePathList(general_t_includepath,includesearchpath);
  235. WritePathList(general_t_librarypath,librarysearchpath);
  236. WritePathList(general_t_objectpath,objectsearchpath);
  237. { Compile the program }
  238. {$ifdef PREPROCWRITE}
  239. if parapreprocess then
  240. parser.preprocess(inputfilepath+inputfilename)
  241. else
  242. {$endif PREPROCWRITE}
  243. parser.compile(inputfilepath+inputfilename);
  244. { Show statistics }
  245. if status.errorcount=0 then
  246. begin
  247. totaltime:=getrealtime-starttime;
  248. if totaltime<0 then
  249. totaltime:=totaltime+3600.0*24.0;
  250. if round(frac(totaltime)*10) >= 10 then
  251. totaltime:=trunc(totaltime) + 1;
  252. timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
  253. if status.codesize<>aword(-1) then
  254. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  255. else
  256. linkstr:='';
  257. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  258. if (Status.Verbosity and V_Warning = V_Warning) and
  259. (Status.CountWarnings <> 0) then
  260. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  261. if (Status.Verbosity and V_Hint = V_Hint) and
  262. (Status.CountHints <> 0) then
  263. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  264. if (Status.Verbosity and V_Note = V_Note) and
  265. (Status.CountNotes <> 0) then
  266. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  267. end;
  268. finally
  269. { no message possible after this !! }
  270. DoneCompiler;
  271. SetExceptionMask(ExceptionMask);
  272. end;
  273. DoneVerbose;
  274. except
  275. on EControlCAbort do
  276. begin
  277. try
  278. { in case of 50 errors, this could cause another exception,
  279. suppress this exception
  280. }
  281. Message(general_f_compilation_aborted);
  282. except
  283. on ECompilerAbort do
  284. ;
  285. end;
  286. DoneVerbose;
  287. end;
  288. on ECompilerAbort do
  289. begin
  290. try
  291. { in case of 50 errors, this could cause another exception,
  292. suppress this exception
  293. }
  294. Message(general_f_compilation_aborted);
  295. except
  296. on ECompilerAbort do
  297. ;
  298. end;
  299. DoneVerbose;
  300. end;
  301. on ECompilerAbortSilent do
  302. begin
  303. DoneVerbose;
  304. end;
  305. on EOutOfMemory do
  306. begin
  307. try
  308. Message(general_f_no_memory_left);
  309. except
  310. on ECompilerAbort do
  311. ;
  312. end;
  313. DoneVerbose;
  314. end;
  315. on e : EInOutError do
  316. begin
  317. try
  318. Message1(general_f_ioerror,e.message);
  319. except
  320. on ECompilerAbort do
  321. ;
  322. end;
  323. DoneVerbose;
  324. end;
  325. on e : EOSError do
  326. begin
  327. try
  328. Message1(general_f_oserror,e.message);
  329. except
  330. on ECompilerAbort do
  331. ;
  332. end;
  333. DoneVerbose;
  334. end;
  335. on Exception do
  336. begin
  337. { General catchall, normally not used }
  338. try
  339. { in case of 50 errors, this could cause another exception,
  340. suppress this exception
  341. }
  342. Message(general_f_compilation_aborted);
  343. except
  344. on ECompilerAbort do
  345. ;
  346. end;
  347. DoneVerbose;
  348. Raise;
  349. end;
  350. end;
  351. {$ifdef SHOWUSEDMEM}
  352. hstatus:=GetFPCHeapStatus;
  353. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  354. {$endif SHOWUSEDMEM}
  355. { Set the return value if an error has occurred }
  356. if status.errorcount=0 then
  357. result:=0
  358. else
  359. result:=1;
  360. end;
  361. end.