FoxCompiler.Mod 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. MODULE Compiler; (** AUTHOR "fof & fn"; PURPOSE "Oberon Compiler Command Interface"; **)
  2. (* (c) fof ETH Zürich, 2008 *)
  3. IMPORT
  4. Basic := FoxBasic, Scanner := FoxScanner, Parser := FoxParser,
  5. SemanticChecker := FoxSemanticChecker, SyntaxTree := FoxSyntaxTree, Formats := FoxFormats,
  6. Streams, Commands,Diagnostics, Options, Kernel, Printout := FoxPrintout, Backend := FoxBackend,Strings, Global := FoxGlobal,
  7. ActiveCells := FoxActiveCells, Hardware := FoxHardware, Frontend := FoxFrontend,
  8. Files;
  9. CONST
  10. (* flags *)
  11. Print* = 0;
  12. Silent* = 1;
  13. Check* = 2;
  14. TraceError* = 3;
  15. Info* = 4;
  16. FindPC* = 5;
  17. ActiveCellsFlag*=6;
  18. Warnings*=7;
  19. ForceModuleBodies*=8;
  20. UseDarwinCCalls*=9; (* use Darwin stack alignment for ext. C procedures *) (*fld*)
  21. SingleModule*=10;
  22. Oberon07*=11;
  23. ChangeCase*=12;
  24. Cooperative*=13;
  25. CellsAreObjects*=14;
  26. DefaultBackend = "AMD";
  27. DefaultFrontend = "Oberon";
  28. TYPE
  29. SectionName = ARRAY 256 OF CHAR; (*! move *)
  30. CompilerOptions*= RECORD
  31. flags*: SET;
  32. frontend*: Frontend.Frontend;
  33. backend*: Backend.Backend;
  34. symbolFile*: Formats.SymbolFileFormat;
  35. objectFile*: Formats.ObjectFileFormat;
  36. hardware*: Hardware.Description;
  37. findPC*: SectionName;
  38. documentation*: Backend.Backend;
  39. activeCellsBackend, activeCellsAssembler: Backend.Backend;
  40. srcPath, destPath: Files.FileName;
  41. replacements: SemanticChecker.Replacement;
  42. END;
  43. PROCEDURE ParseReplacements(CONST filename: ARRAY OF CHAR; VAR replacement: SemanticChecker.Replacement; diagnostics: Diagnostics.Diagnostics): BOOLEAN;
  44. VAR reader: Streams.Reader; r: SemanticChecker.Replacement;
  45. identifier: SyntaxTree.QualifiedIdentifier;
  46. scanner: Scanner.Scanner; parser: Parser.Parser; expression: SyntaxTree.Expression;
  47. BEGIN
  48. replacement := NIL;
  49. reader := Basic.GetFileReader(filename);
  50. IF reader = NIL THEN
  51. diagnostics.Error (filename, Diagnostics.Invalid, Diagnostics.Invalid, "failed to open");
  52. ELSE
  53. NEW(scanner, filename, reader,0, diagnostics);
  54. NEW(parser, scanner, diagnostics);
  55. REPEAT (* WHILE parser.Peek(Scanner.Identifier) DO*)
  56. identifier := parser.QualifiedIdentifier();
  57. IF parser.Mandatory(Scanner.Equal) THEN
  58. expression := parser.Expression();
  59. NEW(r); identifier.GetName(r.name); r.expression := expression; r.used := FALSE;
  60. r.next := replacement; replacement := r;
  61. END;
  62. WHILE parser.Optional(Scanner.Semicolon) DO END;
  63. UNTIL ~parser.Peek(Scanner.Identifier); (*END;*)
  64. END;
  65. (*done := FALSE;
  66. WHILE reader.GetString(name) & ~done DO
  67. IF reader.GetChar(equal) & (equal = "=") & reader.GetString(value) THEN
  68. NEW(r); r.name := name; r.string := Strings.NewString(value); r.used := FALSE;
  69. r.next := replacement; replacement := r;
  70. ELSE done := TRUE;
  71. END;
  72. END;
  73. *)
  74. RETURN (replacement # NIL)
  75. END ParseReplacements;
  76. PROCEDURE Modules*(CONST source: ARRAY OF CHAR; (* source file name, for debugging and better error reports *)
  77. reader: Streams.Reader; (* reader to read from *)
  78. position: LONGINT; (* starting position in reader *)
  79. diagnostics: Diagnostics.Diagnostics; (* error output and status report *)
  80. log: Streams.Writer;
  81. CONST options: CompilerOptions;
  82. VAR importCache: SyntaxTree.ModuleScope): BOOLEAN;
  83. VAR
  84. module: SyntaxTree.Module;
  85. checker: SemanticChecker.Checker;
  86. warnings: SemanticChecker.Warnings;
  87. printer: Printout.Printer;
  88. system: Global.System;
  89. generatedModule: Formats.GeneratedModule;
  90. name: SyntaxTree.IdentifierString;
  91. split: Strings.StringArray;
  92. sectionOffset: LONGINT;
  93. activeCellsSpecification: ActiveCells.Specification;
  94. flags: SET;
  95. PROCEDURE FinalMessage(error: BOOLEAN; CONST msg: ARRAY OF CHAR);
  96. VAR message,name: ARRAY 256 OF CHAR;
  97. BEGIN
  98. message := "";
  99. IF (module # NIL) & (module.context # SyntaxTree.invalidIdentifier) THEN
  100. Basic.GetString(module.context,message);
  101. Strings.Append (message, ".");
  102. ELSE
  103. message := "";
  104. END;
  105. IF (module # NIL) & (module.name # SyntaxTree.invalidIdentifier) THEN
  106. Basic.GetString(module.name,name);
  107. Strings.Append (message, name);
  108. END;
  109. Strings.Append (message, msg);
  110. IF error THEN
  111. IF diagnostics # NIL THEN
  112. diagnostics.Error (source, Diagnostics.Invalid, Diagnostics.Invalid, message);
  113. END;
  114. ELSE
  115. IF (log # NIL) & ~(Silent IN options.flags) & ~(FindPC IN options.flags) THEN
  116. log.String("compiling ");
  117. IF source # "" THEN log.String(source); log.String(" => "); END;
  118. log.String(message); log.Ln;
  119. log.Update;
  120. END;
  121. END;
  122. END FinalMessage;
  123. PROCEDURE PrintModule;
  124. VAR print: Streams.Writer;
  125. BEGIN
  126. print := Basic.GetWriter(Basic.GetDebugWriter("Compiler Debug Output"));
  127. IF Info IN options.flags THEN
  128. printer := Printout.NewPrinter(print,Printout.All,Info IN options.flags);
  129. ELSE
  130. printer := Printout.NewPrinter(print,Printout.SourceCode,Info IN options.flags);
  131. END;
  132. print.Ln; printer.Module(module); print.Ln;
  133. print.Update;
  134. END PrintModule;
  135. BEGIN
  136. flags := options.flags;
  137. IF options.findPC # "" THEN EXCL(flags, Warnings) END;
  138. IF TraceError IN options.flags THEN
  139. diagnostics := Basic.GetTracingDiagnostics(diagnostics)
  140. END;
  141. IF options.backend = NIL THEN
  142. system := Global.DefaultSystem()
  143. ELSE
  144. IF Oberon07 IN options.flags THEN options.backend.SetOberon07 END; (* inform the backend about that the Oberon07 mode, it will return the corresponding Sytem object *)
  145. system := options.backend.GetSystem();
  146. END;
  147. system.SetCellsAreObjects(CellsAreObjects IN flags);
  148. IF (options.objectFile # NIL) & (options.objectFile.ForceModuleBodies()) THEN INCL(flags, ForceModuleBodies) END;
  149. IF (ActiveCellsFlag IN flags) & ~(CellsAreObjects IN flags) THEN
  150. NEW(activeCellsSpecification, "", diagnostics, log);
  151. IF (system # NIL) THEN
  152. activeCellsSpecification.DefineDevices(system);
  153. Global.OperatorDefined(system, Scanner.Questionmarks, TRUE);
  154. END;
  155. IF options.activeCellsBackend = NIL THEN FinalMessage(TRUE,"could not install activeCells backend"); RETURN FALSE END;
  156. END;
  157. options.frontend.Initialize(diagnostics, reader, source, position, ActiveCellsFlag IN flags);
  158. REPEAT
  159. (** first phase: scan and parse **)
  160. module := options.frontend.Parse();
  161. IF options.frontend.Error() THEN
  162. FinalMessage(TRUE," could not be compiled (parser errors).");
  163. RETURN FALSE;
  164. END;
  165. ASSERT(module # NIL);
  166. IF Check IN flags THEN
  167. (** second phase: check and resolve symbols **)
  168. IF (options.symbolFile # NIL) THEN
  169. options.symbolFile.Initialize(diagnostics,system,options.destPath);
  170. END;
  171. checker := SemanticChecker.NewChecker(diagnostics,Info IN flags,UseDarwinCCalls IN flags,Cooperative IN flags,system,options.symbolFile,activeCellsSpecification,importCache);
  172. checker.replacements := options.replacements;
  173. checker.Module(module);
  174. IF checker.error THEN
  175. FinalMessage(TRUE," could not be compiled (checker errors).");
  176. RETURN FALSE
  177. ELSIF Warnings IN flags THEN
  178. warnings := SemanticChecker.NewWarnings(diagnostics);
  179. warnings.Module(module);
  180. END;
  181. IF Print IN flags THEN
  182. IF ChangeCase IN flags THEN module.SetCase(1-module.case) END;
  183. PrintModule;
  184. IF ChangeCase IN flags THEN module.SetCase(1-module.case) END;
  185. END;
  186. IF (ActiveCellsFlag IN flags) & ~(CellsAreObjects IN flags) THEN
  187. Global.GetSymbolName(module,name);
  188. activeCellsSpecification.Init(name,diagnostics,log)
  189. END;
  190. (** third phase: generate code, can consist of sub-phases (such as intermediate backend / hardware backend) **)
  191. IF options.backend # NIL THEN
  192. options.backend.Initialize(diagnostics, log, flags, checker, system, activeCellsSpecification);
  193. IF options.findPC # "" THEN
  194. split := Strings.Split(options.findPC,":");
  195. IF LEN(split)>1 THEN
  196. Strings.StrToInt(split[1]^,sectionOffset);
  197. options.backend.FindPC(module, split[0]^,sectionOffset);
  198. IF options.backend.error THEN
  199. FinalMessage(TRUE," could not be compiled (backend errors).");
  200. RETURN FALSE
  201. ELSE
  202. RETURN TRUE
  203. END;
  204. END;
  205. END;
  206. generatedModule := options.backend.ProcessSyntaxTreeModule(module);
  207. IF options.backend.error THEN
  208. FinalMessage(TRUE, " could not be compiled (backend errors).");
  209. RETURN FALSE
  210. END;
  211. END;
  212. (** generate symbol file **)
  213. IF (options.symbolFile # NIL) & ~options.symbolFile.Export(module, importCache) THEN
  214. FinalMessage(TRUE, " could not be compiled (symbol File errors).");
  215. RETURN FALSE
  216. END;
  217. (** generate object file **)
  218. IF options.objectFile # NIL THEN
  219. options.objectFile.Initialize(diagnostics, options.destPath);
  220. IF options.findPC # "" THEN
  221. Strings.StrToInt(options.findPC, sectionOffset);
  222. generatedModule.SetFindPC(sectionOffset);
  223. END;
  224. IF generatedModule = NIL THEN
  225. FinalMessage(TRUE, " could not write object file (nothing generated).");
  226. RETURN FALSE
  227. ELSIF ~options.objectFile.Export(generatedModule,options.symbolFile) THEN
  228. FinalMessage(TRUE, " could not be compiled (object file errors).");
  229. RETURN FALSE
  230. END;
  231. END;
  232. IF activeCellsSpecification # NIL THEN
  233. options.activeCellsBackend.Initialize(diagnostics,log, flags,checker,system,activeCellsSpecification);
  234. generatedModule := options.activeCellsBackend.ProcessSyntaxTreeModule(module);
  235. IF options.activeCellsBackend.error THEN
  236. FinalMessage(TRUE, " could not be compiled (activeCells backend errors)");
  237. RETURN FALSE
  238. END;
  239. END;
  240. IF activeCellsSpecification = NIL THEN (* no activeCells *)
  241. ELSIF (activeCellsSpecification.types.Length() = 0) & (activeCellsSpecification.instances.Length()=0) THEN (* nothing defined *)
  242. ELSE
  243. IF options.activeCellsAssembler= NIL THEN FinalMessage(TRUE,"could not install activeCells assembler"); RETURN FALSE END;
  244. options.activeCellsAssembler.Initialize(diagnostics, log, flags, checker, system, activeCellsSpecification);
  245. IF options.hardware # NIL THEN options.hardware.Init(diagnostics, log) END;
  246. IF ~options.activeCellsAssembler.Emit(options.backend) THEN
  247. (*activeCellsSpecification.Link(diagnostics,system.codeUnit, system.dataUnit) *)
  248. FinalMessage(TRUE, " could not assemble"); RETURN FALSE
  249. ELSIF ~activeCellsSpecification.Emit() THEN
  250. FinalMessage(TRUE, " could not emit backend specification"); RETURN FALSE;
  251. ELSIF (options.hardware # NIL) & ~options.hardware.Emit(activeCellsSpecification) THEN
  252. FinalMessage(TRUE, " could not emit hardware"); RETURN FALSE;
  253. END;
  254. END;
  255. IF options.documentation # NIL THEN
  256. options.documentation.Initialize(diagnostics,log, flags,checker,system,activeCellsSpecification);
  257. generatedModule := options.documentation.ProcessSyntaxTreeModule(module);
  258. END;
  259. FinalMessage(FALSE, " done.");
  260. ELSIF Print IN flags THEN
  261. IF ChangeCase IN flags THEN module.SetCase(1-module.case) END;
  262. PrintModule;
  263. FinalMessage(FALSE, " done.")
  264. ELSE
  265. FinalMessage(FALSE, " done.");
  266. END;
  267. UNTIL (SingleModule IN flags) OR options.frontend.Done();
  268. RETURN TRUE;
  269. END Modules;
  270. PROCEDURE GetDefaults(CONST name: ARRAY OF CHAR): Streams.Reader;
  271. VAR s: Streams.StringReader; platform: Platform;
  272. BEGIN
  273. s := NIL;
  274. platform := GetPlatform(name);
  275. IF platform # NIL THEN
  276. NEW(s, LEN(platform.defaults));
  277. s.Set(platform.defaults);
  278. END;
  279. RETURN s;
  280. END GetDefaults;
  281. PROCEDURE GetOptions*(input: Streams.Reader; error:Streams.Writer; diagnostics: Diagnostics.Diagnostics;
  282. VAR compilerOptions: CompilerOptions): BOOLEAN;
  283. VAR options: Options.Options; name: ARRAY 256 OF CHAR; result: BOOLEAN; position: LONGINT;
  284. defaults: Streams.Reader;
  285. parsed: BOOLEAN;
  286. PROCEDURE Error(CONST error: ARRAY OF CHAR);
  287. BEGIN
  288. IF diagnostics # NIL THEN
  289. diagnostics.Error("",Diagnostics.Invalid,Diagnostics.Invalid,error);
  290. END;
  291. END Error;
  292. BEGIN
  293. result := TRUE;
  294. NEW(options);
  295. options.Add("p","platform",Options.String);
  296. options.Add(0X,"showOptions",Options.Flag);
  297. options.Add(0X,"print",Options.Flag);
  298. options.Add(0X,"Print",Options.Flag);
  299. options.Add(0X,"silent",Options.Flag);
  300. options.Add("c","check",Options.Flag);
  301. options.Add("e","traceError",Options.Flag);
  302. options.Add("I","interface",Options.Flag);
  303. options.Add("i","info",Options.Flag);
  304. options.Add(0X,"oberon07",Options.Flag);
  305. options.Add("b","backend",Options.String);
  306. options.Add("F","frontEnd",Options.String);
  307. options.Add("f","findPC",Options.String);
  308. options.Add(0X,"singleModule",Options.Flag);
  309. options.Add(0X, "symbolFile", Options.String);
  310. options.Add(0X, "objectFile", Options.String);
  311. options.Add(0X,"activeCells", Options.Flag);
  312. options.Add("w","warnings", Options.Flag);
  313. options.Add(0X,"darwinHost", Options.Flag);
  314. options.Add(0X,"hardware", Options.String);
  315. options.Add("d","documentation", Options.String);
  316. options.Add("S","srcPath", Options.String);
  317. options.Add("D","destPath", Options.String);
  318. options.Add(0X,"replacements", Options.String);
  319. options.Add(0X,"cooperative", Options.Flag);
  320. position := input.Pos();
  321. parsed := options.Parse(input,NIL);
  322. IF options.GetString("platform", name) THEN
  323. defaults := GetDefaults(name);
  324. IF defaults = NIL THEN
  325. error.String("Unknown platform"); error.Ln
  326. ELSE
  327. TRACE(defaults);
  328. parsed := options.Parse(defaults, NIL) & parsed;
  329. input.SetPos(position);
  330. parsed := options.Parse(input, NIL) & parsed; (* reparse overwrites *)
  331. END;
  332. ELSE
  333. defaults := NIL;
  334. END;
  335. IF options.GetString("b", name) THEN
  336. IF name = "" THEN compilerOptions.backend := NIL
  337. ELSE
  338. compilerOptions.backend := Backend.GetBackendByName(name);
  339. IF (compilerOptions.backend = NIL) THEN
  340. Error("backend could not be installed"); result := FALSE;
  341. END;
  342. END;
  343. ELSE compilerOptions.backend := Backend.GetBackendByName(DefaultBackend);
  344. IF compilerOptions.backend = NIL THEN Error("default backend could not be installed"); result := FALSE END;
  345. END;
  346. IF options.GetString("F", name) THEN
  347. IF name = "" THEN compilerOptions.frontend := NIL
  348. ELSE
  349. compilerOptions.frontend := Frontend.GetFrontendByName(name);
  350. IF (compilerOptions.frontend = NIL) THEN
  351. Error("backend could not be installed"); result := FALSE;
  352. END;
  353. END;
  354. ELSE compilerOptions.frontend := Frontend.GetFrontendByName(DefaultFrontend);
  355. IF compilerOptions.frontend = NIL THEN Error("default frontend could not be installed"); result := FALSE END;
  356. END;
  357. IF options.GetString("objectFile",name) THEN
  358. IF name = "" THEN compilerOptions.objectFile := NIL
  359. ELSE
  360. compilerOptions.objectFile := Formats.GetObjectFileFormat(name);
  361. IF compilerOptions.objectFile = NIL THEN Error("object file format could not be installed"); result := FALSE END;
  362. END;
  363. ELSIF compilerOptions.backend # NIL THEN
  364. compilerOptions.objectFile := compilerOptions.backend.DefaultObjectFileFormat();
  365. END;
  366. IF options.GetString("symbolFile",name) THEN
  367. IF name = "" THEN compilerOptions.symbolFile := NIL
  368. ELSE
  369. compilerOptions.symbolFile := Formats.GetSymbolFileFormat(name);
  370. IF compilerOptions.symbolFile = NIL THEN Error("symbol file format could not be installed"); result := FALSE END;
  371. END;
  372. ELSIF compilerOptions.backend # NIL THEN
  373. compilerOptions.symbolFile := compilerOptions.backend.DefaultSymbolFileFormat();
  374. IF (compilerOptions.symbolFile = NIL) & (compilerOptions.objectFile # NIL) THEN
  375. compilerOptions.symbolFile := compilerOptions.objectFile.DefaultSymbolFileFormat();
  376. END;
  377. ELSIF compilerOptions.objectFile # NIL THEN
  378. compilerOptions.symbolFile := compilerOptions.objectFile.DefaultSymbolFileFormat();
  379. END;
  380. IF options.GetString("hardware",name) THEN
  381. compilerOptions.hardware := Hardware.GetDescription(name);
  382. IF compilerOptions.hardware = NIL THEN
  383. Error("hardware description could not be installed"); result := FALSE;
  384. END;
  385. END;
  386. IF options.GetString("d", name) THEN
  387. compilerOptions.documentation := Backend.GetBackendByName("Documentation");
  388. IF (compilerOptions.documentation = NIL) THEN
  389. Error("documentation engine could not be installed"); result := FALSE;
  390. END;
  391. ELSE
  392. compilerOptions.documentation := NIL
  393. END;
  394. IF options.GetFlag("activeCells") THEN
  395. compilerOptions.activeCellsBackend := Backend.GetBackendByName("FoxActiveCellsBackend");
  396. compilerOptions.activeCellsAssembler := Backend.GetBackendByName("FoxIntermediateLinker");
  397. END;
  398. IF options.GetString("replacements", name) THEN
  399. IF ~ParseReplacements(name, compilerOptions.replacements, diagnostics) THEN
  400. Error("replacement file could not be opened or is empty"); result := FALSE;
  401. END;
  402. ELSE compilerOptions.replacements := NIL
  403. END;
  404. IF compilerOptions.backend # NIL THEN compilerOptions.backend.DefineOptions (options); INCL(compilerOptions.flags,Check); END;
  405. IF compilerOptions.symbolFile # NIL THEN compilerOptions.symbolFile.DefineOptions(options); INCL(compilerOptions.flags,Check) END;
  406. IF compilerOptions.objectFile # NIL THEN compilerOptions.objectFile.DefineOptions(options); INCL(compilerOptions.flags,Check) END;
  407. IF compilerOptions.documentation # NIL THEN compilerOptions.documentation.DefineOptions(options) END;
  408. IF compilerOptions.activeCellsBackend # NIL THEN compilerOptions.activeCellsBackend.DefineOptions(options) END;
  409. IF compilerOptions.activeCellsAssembler # NIL THEN compilerOptions.activeCellsAssembler.DefineOptions(options) END;
  410. IF result & ~parsed THEN
  411. options.Clear;
  412. IF defaults # NIL THEN
  413. defaults.SetPos(0);
  414. parsed := options.Parse(defaults, error);
  415. END;
  416. input.SetPos(position);
  417. result := options.Parse(input,error)
  418. END;
  419. IF result THEN
  420. IF options.GetFlag("print") THEN INCL(compilerOptions.flags, Print) END;
  421. IF options.GetFlag("Print") THEN INCL(compilerOptions.flags, Print); INCL(compilerOptions.flags, ChangeCase) END;
  422. IF options.GetFlag("silent") THEN INCL(compilerOptions.flags, Silent) END;
  423. IF options.GetFlag("check") THEN INCL(compilerOptions.flags, Check) END;
  424. IF options.GetFlag("traceError") THEN INCL(compilerOptions.flags, TraceError) END;
  425. IF options.GetFlag("info") THEN INCL(compilerOptions.flags,Info) END;
  426. IF options.GetString("findPC",compilerOptions.findPC) THEN INCL(compilerOptions.flags,FindPC) END;
  427. IF options.GetFlag("warnings") THEN INCL(compilerOptions.flags, Warnings) END;
  428. IF options.GetFlag("darwinHost") THEN INCL(compilerOptions.flags,UseDarwinCCalls) END; (*fld*)
  429. IF options.GetFlag("singleModule") THEN INCL(compilerOptions.flags,SingleModule) END;
  430. IF options.GetFlag("oberon07") THEN INCL(compilerOptions.flags, Oberon07) END;
  431. IF options.GetFlag("activeCells") THEN INCL(compilerOptions.flags, ActiveCellsFlag) END;
  432. IF options.GetFlag("cooperative") THEN INCL(compilerOptions.flags, Cooperative) END;
  433. IF options.GetFlag("cellsAreObjects") THEN INCL(compilerOptions.flags, CellsAreObjects) END;
  434. IF ~options.GetString("srcPath", compilerOptions.srcPath) THEN compilerOptions.srcPath := "" END;
  435. IF ~options.GetString("destPath", compilerOptions.destPath) THEN compilerOptions.destPath := "" END;
  436. IF compilerOptions.backend # NIL THEN compilerOptions.backend.GetOptions (options) END;
  437. IF compilerOptions.symbolFile # NIL THEN compilerOptions.symbolFile.GetOptions(options) END;
  438. IF compilerOptions.objectFile # NIL THEN compilerOptions.objectFile.GetOptions(options) END;
  439. IF compilerOptions.documentation # NIL THEN compilerOptions.documentation.GetOptions(options) END;
  440. IF compilerOptions.activeCellsBackend # NIL THEN compilerOptions.activeCellsBackend.GetOptions(options) END;
  441. IF compilerOptions.activeCellsAssembler # NIL THEN compilerOptions.activeCellsAssembler.GetOptions(options) END;
  442. END;
  443. IF options.GetFlag("showOptions") THEN options.Show(error) END;
  444. RETURN result
  445. END GetOptions;
  446. PROCEDURE Compile*(context : Commands.Context);
  447. VAR
  448. filename, path, file: Files.FileName;
  449. error: BOOLEAN;
  450. diagnostics: Diagnostics.Diagnostics;
  451. time: LONGINT; reader: Streams.Reader;
  452. importCache: SyntaxTree.ModuleScope;
  453. options: CompilerOptions;
  454. replacement: SemanticChecker.Replacement;
  455. name: ARRAY 128 OF CHAR;
  456. BEGIN
  457. error := FALSE;
  458. diagnostics := Basic.GetDiagnostics(context.error);
  459. IF GetOptions(context.arg,context.error,diagnostics,options) THEN
  460. time := Kernel.GetTicks();
  461. WHILE Basic.GetStringParameter(context.arg,filename) & ~error DO
  462. IF options.srcPath # "" THEN
  463. Files.SplitPath(filename, path, file);
  464. IF path = "" THEN Files.JoinPath(options.srcPath, file, filename) END;
  465. END;
  466. reader := Basic.GetFileReader(filename);
  467. IF reader = NIL THEN
  468. diagnostics.Error (filename, Diagnostics.Invalid, Diagnostics.Invalid, "failed to open"); error := TRUE;
  469. ELSE
  470. error := ~Modules(filename, reader, 0, diagnostics,context.out, options, importCache);
  471. END;
  472. context.out.Update;
  473. context.error.Update;
  474. END;
  475. IF Silent IN options.flags THEN
  476. time := Kernel.GetTicks()-time;
  477. context.out.Ln; context.out.String("compiler elapsed ms"); context.out.Int(time,10);
  478. END;
  479. IF ~error THEN
  480. replacement := options.replacements;
  481. WHILE replacement # NIL DO
  482. IF ~replacement.used THEN
  483. name := replacement.name;
  484. diagnostics.Warning(name, Diagnostics.Invalid, Diagnostics.Invalid, " unused replacement.");
  485. END;
  486. replacement := replacement.next;
  487. END;
  488. END;
  489. END;
  490. IF error THEN context.result := -1 ELSE context.result := Commands.Ok END;
  491. END Compile;
  492. PROCEDURE CompileReader*(context: Commands.Context; reader: Streams.Reader);
  493. VAR
  494. filename: ARRAY 256 OF CHAR;
  495. error: BOOLEAN;
  496. diagnostics: Diagnostics.Diagnostics;
  497. importCache: SyntaxTree.ModuleScope;
  498. options: CompilerOptions;
  499. BEGIN
  500. error := FALSE;
  501. diagnostics := Basic.GetDiagnostics(context.error);
  502. IF GetOptions(context.arg,context.error,diagnostics,options) THEN
  503. IF reader = NIL THEN
  504. diagnostics.Error (filename, Diagnostics.Invalid, Diagnostics.Invalid, "failed to open"); error := TRUE;
  505. ELSE
  506. error := ~Modules(filename, reader, 0, diagnostics, context.out, options, importCache);
  507. END;
  508. context.out.Update;
  509. END;
  510. END CompileReader;
  511. TYPE
  512. Platform = POINTER TO RECORD
  513. name: ARRAY 32 OF CHAR;
  514. defaults: ARRAY 256 OF CHAR;
  515. END;
  516. Platforms = OBJECT(Basic.List)
  517. PROCEDURE GetPlatform(i: LONGINT): Platform;
  518. BEGIN
  519. RETURN Get(i)(Platform);
  520. END GetPlatform;
  521. END Platforms;
  522. VAR platforms: Platforms;
  523. PROCEDURE AddPlatform(CONST name: ARRAY OF CHAR; CONST defaults: ARRAY OF CHAR);
  524. VAR i: LONGINT; platform: Platform;
  525. BEGIN
  526. platform := GetPlatform(name);
  527. IF platform = NIL THEN
  528. NEW(platform);
  529. COPY(name, platform.name);
  530. platforms.Add(platform);
  531. END;
  532. COPY(defaults, platform.defaults);
  533. END AddPlatform;
  534. PROCEDURE GetPlatform(CONST name: ARRAY OF CHAR): Platform;
  535. VAR i: LONGINT; platform: Platform;
  536. BEGIN
  537. FOR i := 0 TO platforms.Length()-1 DO
  538. platform := platforms.GetPlatform(i);
  539. IF platform.name = name THEN
  540. RETURN platform
  541. END;
  542. END;
  543. RETURN NIL;
  544. END GetPlatform;
  545. BEGIN
  546. NEW(platforms,8);
  547. (* platform definitions hard coded for the common cases -- maybe (parts of it) should be outsourced to a file ?*)
  548. AddPlatform("Win32","-b=AMD --objectFile=Binary --symbolFile=Binary --objectFileExtensions=.Obw --symbolFileExtension=.Obw");
  549. AddPlatform("Win32G","-b=AMD --objectFile=Generic --symbolFile=Textual --newObjectFile --mergeSections --objectFileExtension=.GofW --symbolFileExtension=.SymW");
  550. AddPlatform("Minos","-b=ARM --objectFile=Generic --newObjectFile --metaData=simple --objectFileExtension=.Goa --symbolFileExtension=.Sya");
  551. AddPlatform("TRM","-b=TRM --objectFile=Generic --newObjectFile --metaData=simple --objectFileExtension=.GofT --symbolFileExtension=.SymT");
  552. AddPlatform("TRMI","-b=TRM --objectFile=Intermediate --newObjectFile --metaData=simple --objectFileExtension=.IroT --symbolFileExtension=.IrsT");
  553. AddPlatform("A2","-b=AMD --objectFile=Binary --objectFileExtension=.Obx --symbolFileExtension=.Obx");
  554. AddPlatform("A2G","-b=AMD --objectFile=Generic --newObjectFile --mergeSections --objectFileExtension=.GofG --symbolFileExtension=.SymG");
  555. AddPlatform("A2Coop","-b=AMD --cooperative --objectFile=Generic --newObjectFile --traceModule=Trace --mergeSections");
  556. END Compiler.