switches.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements the parsing of the switches like $I-
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit switches;
  18. {$i fpcdefs.inc}
  19. interface
  20. procedure HandleSwitch(switch,state:char);
  21. function CheckSwitch(switch,state:char):boolean;
  22. implementation
  23. uses
  24. globtype,systems,cpuinfo,
  25. globals,verbose,fmodule;
  26. {****************************************************************************
  27. Main Switches Parsing
  28. ****************************************************************************}
  29. type
  30. TSwitchType=(ignoredsw,localsw,modulesw,globalsw,illegalsw,unsupportedsw,alignsw,optimizersw,packenumsw,pentiumfdivsw);
  31. SwitchRec=record
  32. typesw : TSwitchType;
  33. setsw : byte;
  34. end;
  35. SwitchRecTable = array['A'..'Z'] of SwitchRec;
  36. const
  37. turboSwitchTable: SwitchRecTable =(
  38. {A} (typesw:alignsw; setsw:ord(cs_localnone)),
  39. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  40. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  41. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  42. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  43. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  44. {G} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  45. {H} (typesw:localsw; setsw:ord(cs_ansistrings)),
  46. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  47. {J} (typesw:localsw; setsw:ord(cs_typed_const_writable)),
  48. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  49. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  50. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  51. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  52. {O} (typesw:optimizersw; setsw:ord(cs_opt_none)),
  53. {P} (typesw:modulesw; setsw:ord(cs_openstring)),
  54. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  55. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  56. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  57. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  58. {U} (typesw:pentiumfdivsw; setsw:ord(cs_localnone)),
  59. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  60. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  61. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  62. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  63. {Z} (typesw:packenumsw; setsw:ord(cs_localnone))
  64. );
  65. macSwitchTable: SwitchRecTable =(
  66. {A} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  67. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  68. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  69. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  70. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  71. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  72. {G} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  73. {H} (typesw:localsw; setsw:ord(cs_ansistrings)),
  74. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  75. {J} (typesw:localsw; setsw:ord(cs_external_var)),
  76. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  77. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  78. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  79. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  80. {O} (typesw:optimizersw; setsw:ord(cs_opt_none)),
  81. {P} (typesw:modulesw; setsw:ord(cs_openstring)),
  82. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  83. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  84. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  85. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  86. {U} (typesw:illegalsw; setsw:ord(cs_localnone)),
  87. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  88. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  89. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  90. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  91. {Z} (typesw:localsw; setsw:ord(cs_externally_visible))
  92. );
  93. procedure HandleSwitch(switch,state:char);
  94. var
  95. switchTablePtr: ^SwitchRecTable;
  96. begin
  97. switch:=upcase(switch);
  98. { Is the Switch in the letters ? }
  99. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  100. begin
  101. Message(scan_w_illegal_switch);
  102. exit;
  103. end;
  104. { Select switch table }
  105. if m_mac in current_settings.modeswitches then
  106. switchTablePtr:= @macSwitchTable
  107. else
  108. switchTablePtr:= @turboSwitchTable;
  109. { Handle the switch }
  110. with switchTablePtr^[switch] do
  111. begin
  112. case typesw of
  113. alignsw:
  114. if state='+' then
  115. current_settings.packrecords:=4
  116. else
  117. current_settings.packrecords:=1;
  118. optimizersw :
  119. begin
  120. if state='+' then
  121. current_settings.optimizerswitches:=level2optimizerswitches
  122. else
  123. current_settings.optimizerswitches:=[];
  124. end;
  125. ignoredsw :
  126. Message1(scan_n_ignored_switch,'$'+switch);
  127. illegalsw :
  128. Message1(scan_w_illegal_switch,'$'+switch);
  129. unsupportedsw :
  130. Message1(scan_w_unsupported_switch,'$'+switch);
  131. localsw :
  132. begin
  133. if not localswitcheschanged then
  134. nextlocalswitches:=current_settings.localswitches;
  135. if state='+' then
  136. include(nextlocalswitches,tlocalswitch(setsw))
  137. else
  138. exclude(nextlocalswitches,tlocalswitch(setsw));
  139. localswitcheschanged:=true;
  140. end;
  141. modulesw :
  142. begin
  143. if current_module.in_global then
  144. begin
  145. if state='+' then
  146. include(current_settings.moduleswitches,tmoduleswitch(setsw))
  147. else
  148. begin
  149. { Turning off debuginfo when lineinfo is requested
  150. is not possible }
  151. if not((cs_use_lineinfo in current_settings.globalswitches) and
  152. (tmoduleswitch(setsw)=cs_debuginfo)) then
  153. exclude(current_settings.moduleswitches,tmoduleswitch(setsw));
  154. end;
  155. end
  156. else
  157. Message(scan_w_switch_is_global);
  158. end;
  159. globalsw :
  160. begin
  161. if current_module.in_global and (current_module=main_module) then
  162. begin
  163. if state='+' then
  164. include(current_settings.globalswitches,tglobalswitch(setsw))
  165. else
  166. exclude(current_settings.globalswitches,tglobalswitch(setsw));
  167. end
  168. else
  169. Message(scan_w_switch_is_global);
  170. end;
  171. packenumsw:
  172. begin
  173. if state='-' then
  174. current_settings.packenum:=1
  175. else
  176. current_settings.packenum:=4;
  177. end;
  178. pentiumfdivsw:
  179. begin
  180. { Switch u- means pentium-safe fdiv off -> fpc default. We don't }
  181. { support u+ }
  182. if state='+' then
  183. Message1(scan_w_unsupported_switch,'$'+switch);
  184. end;
  185. end;
  186. end;
  187. end;
  188. function CheckSwitch(switch,state:char):boolean;
  189. var
  190. found : boolean;
  191. switchTablePtr: ^SwitchRecTable;
  192. begin
  193. switch:=upcase(switch);
  194. { Is the Switch in the letters ? }
  195. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  196. begin
  197. Message(scan_w_illegal_switch);
  198. CheckSwitch:=false;
  199. exit;
  200. end;
  201. { Select switch table }
  202. if m_mac in current_settings.modeswitches then
  203. switchTablePtr:= @macSwitchTable
  204. else
  205. switchTablePtr:= @turboSwitchTable;
  206. { Check the switch }
  207. with switchTablePtr^[switch] do
  208. begin
  209. case typesw of
  210. localsw : found:=(tlocalswitch(setsw) in current_settings.localswitches);
  211. modulesw : found:=(tmoduleswitch(setsw) in current_settings.moduleswitches);
  212. globalsw : found:=(tglobalswitch(setsw) in current_settings.globalswitches);
  213. packenumsw : found := (current_settings.packenum = 4);
  214. else
  215. found:=false;
  216. end;
  217. if state='-' then
  218. found:=not found;
  219. CheckSwitch:=found;
  220. end;
  221. end;
  222. end.