FoxBackend.Mod 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. MODULE FoxBackend; (** AUTHOR "kaeserm,fof"; PURPOSE "Oberon Compiler: Common backend module"; **)
  2. IMPORT
  3. Streams, Diagnostics, Global := FoxGlobal, Formats := FoxFormats, SyntaxTree := FoxSyntaxTree, SemanticChecker := FoxSemanticChecker, ActiveCells := FoxActiveCells, Options, Strings;
  4. TYPE
  5. SectionName = ARRAY 256 OF CHAR;
  6. Backend* = OBJECT
  7. VAR
  8. diagnostics-: Diagnostics.Diagnostics;
  9. log-: Streams.Writer;
  10. flags*: SET;
  11. system-: Global.System;
  12. error-: BOOLEAN;
  13. checker-: SemanticChecker.Checker;
  14. source-: SyntaxTree.String;
  15. activeCellsSpecification-: ActiveCells.Specification;
  16. findSectionName-: SectionName;
  17. findSectionOffset-: LONGINT;
  18. capabilities-: SET;
  19. oberon07-: BOOLEAN;
  20. instructionWidth -: LONGINT;
  21. hasLinkRegister-: BOOLEAN;
  22. (* constructor *)
  23. PROCEDURE & InitBackend *;
  24. BEGIN
  25. oberon07 := FALSE;
  26. system := NIL; (* only one instance per backend, never reallocate ! *)
  27. diagnostics := NIL;
  28. flags := {};
  29. error := FALSE;
  30. findSectionName := "";
  31. findSectionOffset := 0;
  32. instructionWidth := -1;
  33. hasLinkRegister := FALSE;
  34. END InitBackend;
  35. PROCEDURE SetOberon07*;
  36. BEGIN
  37. (*
  38. ASSERT(system = NIL); (* assert that system Object has not been requested yet *)
  39. object may be reused!
  40. *)
  41. oberon07 := TRUE
  42. END SetOberon07;
  43. PROCEDURE SetCapabilities*(capabilities: SET);
  44. BEGIN
  45. SELF.capabilities := capabilities;
  46. END SetCapabilities;
  47. PROCEDURE SetHasLinkRegister*;
  48. BEGIN
  49. hasLinkRegister := TRUE;
  50. END SetHasLinkRegister;
  51. PROCEDURE SetInstructionWidth* (instructionWidth: LONGINT);
  52. BEGIN
  53. SELF.instructionWidth := instructionWidth;
  54. (*TRACE(instructionWidth);*)
  55. END SetInstructionWidth;
  56. PROCEDURE ResetError*;
  57. BEGIN error := FALSE
  58. END ResetError;
  59. (* initialize backend for usage *)
  60. PROCEDURE Initialize*(diagnostics: Diagnostics.Diagnostics; log: Streams.Writer; flags: SET; checker: SemanticChecker.Checker; system: Global.System; activeCellsSpecification: ActiveCells.Specification);
  61. BEGIN
  62. error := FALSE;
  63. SELF.diagnostics := diagnostics;
  64. SELF.log := log;
  65. SELF.flags := flags;
  66. SELF.checker := checker;
  67. SELF.system := system;
  68. SELF.activeCellsSpecification := activeCellsSpecification;
  69. END Initialize;
  70. (* Get the system used by this backend (singleton) *)
  71. PROCEDURE GetSystem*():Global.System;
  72. BEGIN
  73. RETURN Global.DefaultSystem();
  74. END GetSystem;
  75. PROCEDURE Error*(CONST source: ARRAY OF CHAR; errorNumber, errorPosition: LONGINT; CONST err: ARRAY OF CHAR);
  76. BEGIN
  77. IF (err # "") & (diagnostics # NIL) THEN
  78. diagnostics.Error(source,errorNumber, errorPosition,err);
  79. END;
  80. error := TRUE;
  81. END Error;
  82. PROCEDURE ProcessSyntaxTreeModule*(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  83. BEGIN RETURN NIL
  84. END ProcessSyntaxTreeModule;
  85. PROCEDURE ProcessIntermediateCodeModule*(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  86. BEGIN RETURN NIL (* only applicable for backends that use an intermediate backend *)
  87. END ProcessIntermediateCodeModule;
  88. (* general emision for chained backends -- used for active cells specification emission *)
  89. PROCEDURE Emit*(backend: Backend): BOOLEAN;
  90. BEGIN RETURN FALSE
  91. END Emit;
  92. PROCEDURE FindPC*(x: SyntaxTree.Module; CONST sectionName: ARRAY OF CHAR; sectionOffset: LONGINT);
  93. BEGIN
  94. END FindPC;
  95. (* code address check method to patch code addresses if in forbidden range (Spartan6!) *)
  96. PROCEDURE CheckCodeAddress*(VAR adr: LONGINT) ;
  97. BEGIN
  98. END CheckCodeAddress;
  99. (* method to query the instruction set description *)
  100. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  101. BEGIN instructionSet := "undefined";
  102. END GetDescription;
  103. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  104. BEGIN RETURN FALSE
  105. END CanPassInRegister;
  106. PROCEDURE DefineOptions*(options: Options.Options);
  107. END DefineOptions;
  108. PROCEDURE GetOptions*(options: Options.Options);
  109. END GetOptions;
  110. PROCEDURE DefaultObjectFileFormat*(): Formats.ObjectFileFormat;
  111. BEGIN RETURN NIL
  112. END DefaultObjectFileFormat;
  113. PROCEDURE DefaultSymbolFileFormat*(): Formats.SymbolFileFormat;
  114. BEGIN RETURN NIL
  115. END DefaultSymbolFileFormat;
  116. END Backend;
  117. PROCEDURE GetDummy*():Backend;
  118. VAR backend: Backend;
  119. BEGIN
  120. NEW(backend);
  121. RETURN backend;
  122. END GetDummy;
  123. PROCEDURE GetBackendByName*(CONST name: ARRAY OF CHAR): Backend;
  124. VAR
  125. procname: ARRAY 256 OF CHAR;
  126. factory: PROCEDURE (): Backend;
  127. backend: Backend;
  128. BEGIN
  129. backend := NIL;
  130. IF Strings.Length(name) > 0 THEN
  131. GETPROCEDURE(name,"Get", factory); (* try long name for example -G=OCERABackend *)
  132. IF factory = NIL THEN (* try short name for example -G=ERA*)
  133. procname := "Fox";
  134. Strings.Append(procname, name);
  135. Strings.Append(procname, "Backend");
  136. GETPROCEDURE(procname,"Get", factory);
  137. END;
  138. IF factory # NIL THEN
  139. backend := factory();
  140. Assert(backend # NIL,"backend factory returned NIL backend");
  141. END;
  142. END;
  143. RETURN backend
  144. END GetBackendByName;
  145. PROCEDURE Assert(b: BOOLEAN; CONST reason: ARRAY OF CHAR);
  146. BEGIN
  147. ASSERT(b);
  148. END Assert;
  149. END FoxBackend.