switches.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. uses
  21. globtype;
  22. procedure HandleSwitch(switch,state:char);
  23. function CheckSwitch(switch,state:char):boolean;
  24. procedure recordpendingverbosityswitch(sw: char; state: char);
  25. procedure recordpendinglocalswitch(sw: tlocalswitch; state: char);
  26. procedure recordpendinglocalfullswitch(const switches: tlocalswitches);
  27. procedure recordpendingverbosityfullswitch(verbosity: longint);
  28. procedure recordpendingcallingswitch(const str: shortstring);
  29. procedure flushpendingswitchesstate;
  30. implementation
  31. uses
  32. systems,cpuinfo,
  33. globals,verbose,comphook,
  34. fmodule;
  35. {****************************************************************************
  36. Main Switches Parsing
  37. ****************************************************************************}
  38. type
  39. TSwitchType=(ignoredsw,localsw,modulesw,globalsw,illegalsw,unsupportedsw,alignsw,optimizersw,packenumsw,pentiumfdivsw);
  40. SwitchRec=record
  41. typesw : TSwitchType;
  42. setsw : byte;
  43. end;
  44. SwitchRecTable = array['A'..'Z'] of SwitchRec;
  45. const
  46. turboSwitchTable: SwitchRecTable =(
  47. {A} (typesw:alignsw; setsw:ord(cs_localnone)),
  48. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  49. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  50. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  51. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  52. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  53. {G} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  54. {H} (typesw:localsw; setsw:ord(cs_ansistrings)),
  55. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  56. {J} (typesw:localsw; setsw:ord(cs_typed_const_writable)),
  57. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  58. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  59. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  60. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  61. {O} (typesw:optimizersw; setsw:ord(cs_opt_none)),
  62. {P} (typesw:modulesw; setsw:ord(cs_openstring)),
  63. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  64. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  65. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  66. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  67. {U} (typesw:pentiumfdivsw; setsw:ord(cs_localnone)),
  68. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  69. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  70. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  71. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  72. {Z} (typesw:packenumsw; setsw:ord(cs_localnone))
  73. );
  74. macSwitchTable: SwitchRecTable =(
  75. {A} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  76. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  77. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  78. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  79. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  80. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  81. {G} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  82. {H} (typesw:localsw; setsw:ord(cs_ansistrings)),
  83. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  84. {J} (typesw:localsw; setsw:ord(cs_external_var)),
  85. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  86. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  87. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  88. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  89. {O} (typesw:optimizersw; setsw:ord(cs_opt_none)),
  90. {P} (typesw:modulesw; setsw:ord(cs_openstring)),
  91. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  92. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  93. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  94. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  95. {U} (typesw:illegalsw; setsw:ord(cs_localnone)),
  96. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  97. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  98. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  99. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  100. {Z} (typesw:localsw; setsw:ord(cs_externally_visible))
  101. );
  102. procedure HandleSwitch(switch,state:char);
  103. var
  104. switchTablePtr: ^SwitchRecTable;
  105. begin
  106. switch:=upcase(switch);
  107. { Is the Switch in the letters ? }
  108. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  109. begin
  110. Message(scan_w_illegal_switch);
  111. exit;
  112. end;
  113. { Select switch table }
  114. if m_mac in current_settings.modeswitches then
  115. switchTablePtr:= @macSwitchTable
  116. else
  117. switchTablePtr:= @turboSwitchTable;
  118. { Handle the switch }
  119. with switchTablePtr^[switch] do
  120. begin
  121. case typesw of
  122. alignsw:
  123. if state='+' then
  124. current_settings.packrecords:=4
  125. else
  126. current_settings.packrecords:=1;
  127. optimizersw :
  128. begin
  129. if state='+' then
  130. current_settings.optimizerswitches:=level2optimizerswitches
  131. else
  132. current_settings.optimizerswitches:=[];
  133. end;
  134. ignoredsw :
  135. Message1(scan_n_ignored_switch,'$'+switch);
  136. illegalsw :
  137. Message1(scan_w_illegal_switch,'$'+switch);
  138. unsupportedsw :
  139. Message1(scan_w_unsupported_switch,'$'+switch);
  140. localsw :
  141. recordpendinglocalswitch(tlocalswitch(setsw),state);
  142. modulesw :
  143. begin
  144. if current_module.in_global then
  145. begin
  146. {$ifndef cpufpemu}
  147. if tmoduleswitch(setsw)=cs_fp_emulation then
  148. begin
  149. Message1(scan_w_unsupported_switch_by_target,'$'+switch);
  150. end
  151. else
  152. {$endif cpufpemu}
  153. begin
  154. if state='+' then
  155. include(current_settings.moduleswitches,tmoduleswitch(setsw))
  156. else
  157. begin
  158. { Turning off debuginfo when lineinfo is requested
  159. is not possible }
  160. if not((cs_use_lineinfo in current_settings.globalswitches) and
  161. (tmoduleswitch(setsw)=cs_debuginfo)) then
  162. exclude(current_settings.moduleswitches,tmoduleswitch(setsw));
  163. end;
  164. end;
  165. end
  166. else
  167. Message(scan_w_switch_is_global);
  168. end;
  169. globalsw :
  170. begin
  171. if current_module.in_global and (current_module=main_module) then
  172. begin
  173. if state='+' then
  174. include(current_settings.globalswitches,tglobalswitch(setsw))
  175. else
  176. exclude(current_settings.globalswitches,tglobalswitch(setsw));
  177. end
  178. else
  179. Message(scan_w_switch_is_global);
  180. end;
  181. packenumsw:
  182. begin
  183. if state='-' then
  184. current_settings.packenum:=1
  185. else
  186. current_settings.packenum:=4;
  187. end;
  188. pentiumfdivsw:
  189. begin
  190. { Switch u- means pentium-safe fdiv off -> fpc default. We don't }
  191. { support u+ }
  192. if state='+' then
  193. Message1(scan_w_unsupported_switch,'$'+switch);
  194. end;
  195. end;
  196. end;
  197. end;
  198. function CheckSwitch(switch,state:char):boolean;
  199. var
  200. found : boolean;
  201. switchTablePtr: ^SwitchRecTable;
  202. begin
  203. switch:=upcase(switch);
  204. { Is the Switch in the letters ? }
  205. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  206. begin
  207. Message(scan_w_illegal_switch);
  208. CheckSwitch:=false;
  209. exit;
  210. end;
  211. { Select switch table }
  212. if m_mac in current_settings.modeswitches then
  213. switchTablePtr:= @macSwitchTable
  214. else
  215. switchTablePtr:= @turboSwitchTable;
  216. { Check the switch }
  217. with switchTablePtr^[switch] do
  218. begin
  219. case typesw of
  220. localsw : found:=(tlocalswitch(setsw) in current_settings.localswitches);
  221. modulesw : found:=(tmoduleswitch(setsw) in current_settings.moduleswitches);
  222. globalsw : found:=(tglobalswitch(setsw) in current_settings.globalswitches);
  223. packenumsw : found := (current_settings.packenum = 4);
  224. else
  225. found:=false;
  226. end;
  227. if state='-' then
  228. found:=not found;
  229. CheckSwitch:=found;
  230. end;
  231. end;
  232. procedure recordpendingverbosityswitch(sw: char; state: char);
  233. begin
  234. pendingstate.nextverbositystr:=pendingstate.nextverbositystr+sw+state;
  235. end;
  236. procedure recordpendinglocalswitch(sw: tlocalswitch; state: char);
  237. begin
  238. if not pendingstate.localswitcheschanged then
  239. pendingstate.nextlocalswitches:=current_settings.localswitches;
  240. if state='-' then
  241. exclude(pendingstate.nextlocalswitches,sw)
  242. else if state='+' then
  243. include(pendingstate.nextlocalswitches,sw)
  244. else { state = '*' }
  245. begin
  246. if sw in init_settings.localswitches then
  247. include(pendingstate.nextlocalswitches,sw)
  248. else
  249. exclude(pendingstate.nextlocalswitches,sw);
  250. end;
  251. pendingstate.localswitcheschanged:=true;
  252. end;
  253. procedure recordpendinglocalfullswitch(const switches: tlocalswitches);
  254. begin
  255. pendingstate.nextlocalswitches:=switches;
  256. pendingstate.localswitcheschanged:=true;
  257. end;
  258. procedure recordpendingverbosityfullswitch(verbosity: longint);
  259. begin
  260. pendingstate.nextverbositystr:='';
  261. pendingstate.nextverbosityfullswitch:=verbosity;
  262. pendingstate.verbosityfullswitched:=true;
  263. end;
  264. procedure recordpendingcallingswitch(const str: shortstring);
  265. begin
  266. pendingstate.nextcallingstr:=str;
  267. end;
  268. procedure flushpendingswitchesstate;
  269. var
  270. tmpproccal: tproccalloption;
  271. begin
  272. { process pending localswitches (range checking, etc) }
  273. if pendingstate.localswitcheschanged then
  274. begin
  275. current_settings.localswitches:=pendingstate.nextlocalswitches;
  276. pendingstate.localswitcheschanged:=false;
  277. end;
  278. { process pending verbosity changes (warnings on, etc) }
  279. if pendingstate.verbosityfullswitched then
  280. begin
  281. status.verbosity:=pendingstate.nextverbosityfullswitch;
  282. pendingstate.verbosityfullswitched:=false;
  283. end;
  284. if pendingstate.nextverbositystr<>'' then
  285. begin
  286. setverbosity(pendingstate.nextverbositystr);
  287. pendingstate.nextverbositystr:='';
  288. end;
  289. { process pending calling convention changes (calling x) }
  290. if pendingstate.nextcallingstr<>'' then
  291. begin
  292. if not SetAktProcCall(pendingstate.nextcallingstr,tmpproccal) then
  293. Message1(parser_w_unknown_proc_directive_ignored,pendingstate.nextcallingstr)
  294. else if not(tmpproccal in supported_calling_conventions) then
  295. Message1(parser_e_illegal_calling_convention,pendingstate.nextcallingstr)
  296. else
  297. current_settings.defproccall:=tmpproccal;
  298. pendingstate.nextcallingstr:='';
  299. end;
  300. end;
  301. end.