2
0

switches.pas 12 KB

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