compiler.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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,cfileutils,cclasses,globals,options,fmodule,parser,symtable,
  34. assemble,link,dbgbase,import,export,tokens,pass_1
  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 win32}
  99. ,i_win
  100. {$endif win32}
  101. ;
  102. function Compile(const cmd:string):longint;
  103. implementation
  104. uses
  105. aasmcpu;
  106. {$if defined(EXTDEBUG) or defined(MEMDEBUG)}
  107. {$define SHOWUSEDMEM}
  108. {$endif}
  109. var
  110. CompilerInitedAfterArgs,
  111. CompilerInited : boolean;
  112. {****************************************************************************
  113. Compiler
  114. ****************************************************************************}
  115. procedure DoneCompiler;
  116. begin
  117. if not CompilerInited then
  118. exit;
  119. { Free compiler if args are read }
  120. if CompilerInitedAfterArgs then
  121. begin
  122. CompilerInitedAfterArgs:=false;
  123. DoneParser;
  124. DoneImport;
  125. DoneExport;
  126. DoneDebuginfo;
  127. DoneLinker;
  128. DoneAssembler;
  129. DoneAsm;
  130. end;
  131. { Free memory for the others }
  132. CompilerInited:=false;
  133. DoneSymtable;
  134. DoneGlobals;
  135. DoneFileUtils;
  136. donetokens;
  137. end;
  138. procedure InitCompiler(const cmd:string);
  139. begin
  140. if CompilerInited then
  141. DoneCompiler;
  142. { inits which need to be done before the arguments are parsed }
  143. InitSystems;
  144. { fileutils depends on source_info so it must be after systems }
  145. InitFileUtils;
  146. { globals depends on source_info so it must be after systems }
  147. InitGlobals;
  148. { verbose depends on exe_path and must be after globals }
  149. InitVerbose;
  150. inittokens;
  151. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  152. CompilerInited:=true;
  153. { this is needed here for the IDE
  154. in case of compilation failure
  155. at the previous compile }
  156. current_module:=nil;
  157. { read the arguments }
  158. read_arguments(cmd);
  159. { inits which depend on arguments }
  160. InitParser;
  161. InitImport;
  162. InitExport;
  163. InitLinker;
  164. InitAssembler;
  165. InitDebugInfo;
  166. InitAsm;
  167. CompilerInitedAfterArgs:=true;
  168. end;
  169. function Compile(const cmd:string):longint;
  170. {$maxfpuregisters 0}
  171. procedure writepathlist(w:longint;l:TSearchPathList);
  172. var
  173. hp : tstringlistitem;
  174. begin
  175. hp:=tstringlistitem(l.first);
  176. while assigned(hp) do
  177. begin
  178. Message1(w,hp.str);
  179. hp:=tstringlistitem(hp.next);
  180. end;
  181. end;
  182. function getrealtime : real;
  183. var
  184. h,m,s,s1000 : word;
  185. begin
  186. DecodeTime(Time,h,m,s,s1000);
  187. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  188. end;
  189. var
  190. starttime : real;
  191. timestr : string[20];
  192. linkstr : string[64];
  193. {$ifdef SHOWUSEDMEM}
  194. hstatus : TFPCHeapStatus;
  195. {$endif SHOWUSEDMEM}
  196. ExceptionMask : TFPUExceptionMask;
  197. begin
  198. try
  199. try
  200. ExceptionMask:=GetExceptionMask;
  201. SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
  202. exOverflow, exUnderflow, exPrecision]);
  203. { Initialize the compiler }
  204. InitCompiler(cmd);
  205. { show some info }
  206. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  207. Message1(general_d_sourceos,source_info.name);
  208. Message1(general_i_targetos,target_info.name);
  209. Message1(general_t_exepath,exepath);
  210. WritePathList(general_t_unitpath,unitsearchpath);
  211. WritePathList(general_t_includepath,includesearchpath);
  212. WritePathList(general_t_librarypath,librarysearchpath);
  213. WritePathList(general_t_objectpath,objectsearchpath);
  214. starttime:=getrealtime;
  215. { Compile the program }
  216. {$ifdef PREPROCWRITE}
  217. if parapreprocess then
  218. parser.preprocess(inputfilepath+inputfilename)
  219. else
  220. {$endif PREPROCWRITE}
  221. parser.compile(inputfilepath+inputfilename);
  222. { Show statistics }
  223. if status.errorcount=0 then
  224. begin
  225. starttime:=getrealtime-starttime;
  226. if starttime<0 then
  227. starttime:=starttime+3600.0*24.0;
  228. timestr:=tostr(trunc(starttime))+'.'+tostr(trunc(frac(starttime)*10));
  229. if status.codesize<>-1 then
  230. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  231. else
  232. linkstr:='';
  233. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  234. if (Status.Verbosity and V_Warning = V_Warning) and
  235. (Status.CountWarnings <> 0) then
  236. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  237. if (Status.Verbosity and V_Hint = V_Hint) and
  238. (Status.CountHints <> 0) then
  239. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  240. if (Status.Verbosity and V_Note = V_Note) and
  241. (Status.CountNotes <> 0) then
  242. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  243. end;
  244. finally
  245. { no message possible after this !! }
  246. DoneCompiler;
  247. SetExceptionMask(ExceptionMask);
  248. end;
  249. DoneVerbose;
  250. except
  251. on EControlCAbort do
  252. begin
  253. try
  254. { in case of 50 errors, this could cause another exception,
  255. suppress this exception
  256. }
  257. Message(general_f_compilation_aborted);
  258. except
  259. on ECompilerAbort do
  260. ;
  261. end;
  262. DoneVerbose;
  263. end;
  264. on ECompilerAbort do
  265. begin
  266. try
  267. { in case of 50 errors, this could cause another exception,
  268. suppress this exception
  269. }
  270. Message(general_f_compilation_aborted);
  271. except
  272. on ECompilerAbort do
  273. ;
  274. end;
  275. DoneVerbose;
  276. end;
  277. on ECompilerAbortSilent do
  278. begin
  279. DoneVerbose;
  280. end;
  281. on Exception do
  282. begin
  283. { General catchall, normally not used }
  284. try
  285. { in case of 50 errors, this could cause another exception,
  286. suppress this exception
  287. }
  288. Message(general_f_compilation_aborted);
  289. except
  290. on ECompilerAbort do
  291. ;
  292. end;
  293. DoneVerbose;
  294. Raise;
  295. end;
  296. end;
  297. {$ifdef SHOWUSEDMEM}
  298. hstatus:=GetFPCHeapStatus;
  299. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  300. {$endif SHOWUSEDMEM}
  301. { Set the return value if an error has occurred }
  302. if status.errorcount=0 then
  303. result:=0
  304. else
  305. result:=1;
  306. end;
  307. end.