switches.pas 12 KB

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