2
0

compiler.pas 8.4 KB

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