1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
%-----------------------------------------------------------------------------%
% Copyright (C) 1994-2000 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
%
% File: ml_switch_gen.m
% Author: fjh (adapted from switch_gen.m)
%
% This module handles the generation of code for switches for the MLDS back-end.
% Switches are disjunctions that do not require backtracking. They are
% detected in switch_detection.m. This is the module that determines what
% sort of indexing to use for each switch and then actually generates the
% code. The code here is quite similar to the code in switch_gen.m, which
% does the same thing for the LLDS back-end.
%
% The following describes the different forms of indexing that we could use.
% Note that currently most of these are not implemented!
% The ones that are not are marked NYI (for "not yet implemented").
%
% 1. For switches on atomic data types (int, char, enums), there are
% several possibilities.
% a) If all the alternative goals for a switch on an atomic data type
% contain only construction unifications of constants, then we
% should generate a dense lookup table (an array) for each output
% variable of the switch, rather than computed goto, so that
% executing the switch becomes a matter of doing an array index for
% each output variable. (NYI)
% b) If the cases are not sparse, and the target supports computed
% gotos, we should use a computed_goto, unless the target supports
% switch statements and the `--prefer-switch' option is set. (NYI)
% c) If the target supports switch statements,
% we generate an MLDS switch statement.
%
% 2. For switches on strings, there are several possibilities.
% a) If the target supports indirect gotos, we should we lookup the
% address to jump to in a hash table (e.g. using open addressing to
% resolve hash collisions), and then jump to it using an indirect
% goto, unless the target supports string switch statements and
% the `--prefer-switch' option is set. (NYI)
% c) If the target supports string switches,
% we generate an MLDS switch statement.
%
% 3. For switches on discriminated union types, we generate code that does
% indexing first on the primary tag, and then on the secondary tag (if
% the primary tag is shared between several function symbols). The
% indexing code for switches on both primary and secondary tags can be
% in the form of a try-me-else chain, a try chain, a dense jump table
% or a binary search. (NYI)
%
% For all other cases (or if the --smart-indexing option was
% disabled), we just generate a chain of if-then-elses.
%
% TODO:
% - implement the things marked NYI above
% - optimize switches so that the recursive case comes first
% (see switch_gen.m).
%
%-----------------------------------------------------------------------------%
:- module ml_switch_gen.
:- interface.
:- import_module prog_data.
:- import_module hlds_goal, hlds_data.
:- import_module code_model.
:- import_module mlds, ml_code_util.
:- import_module globals.
:- import_module list.
% Generate MLDS code for a switch.
%
:- pred ml_gen_switch(prog_var, can_fail, list(case), code_model, prog_context,
mlds__defns, mlds__statements,
ml_gen_info, ml_gen_info).
:- mode ml_gen_switch(in, in, in, in, in, out, out, in, out) is det.
% The following types are exported to the modules that implement
% specialized kinds of switches.
% Generate an appropriate default for a switch.
%
:- pred ml_switch_generate_default(can_fail::in, code_model::in,
prog_context::in, switch_default::out,
ml_gen_info::in, ml_gen_info::out) is det.
% Succeed iff the target supports the specified construct.
:- pred target_supports_int_switch(globals::in) is semidet.
:- pred target_supports_string_switch(globals::in) is semidet.
:- pred target_supports_goto(globals::in) is semidet.
:- pred target_supports_computed_goto(globals::in) is semidet.
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module ml_tag_switch, ml_string_switch.
:- import_module ml_code_gen, ml_unify_gen, ml_code_util, ml_simplify_switch.
:- import_module switch_util, type_util.
:- import_module options.
:- import_module bool, int, string, map, tree, std_util, require.
%-----------------------------------------------------------------------------%
% Choose which method to use to generate the switch.
% CanFail says whether the switch covers all cases.
ml_gen_switch(CaseVar, CanFail, Cases, CodeModel, Context,
MLDS_Decls, MLDS_Statements) -->
%
% Lookup the representation of the constructors for the tag tests
% and their corresponding priorities.
%
ml_switch_lookup_tags(Cases, CaseVar, TaggedCases0),
%
% Sort the cases according to the priority of their tag tests.
%
{ list__sort_and_remove_dups(TaggedCases0, TaggedCases) },
%
% Figure out what kind of switch this is
%
ml_switch_gen__determine_category(CaseVar, SwitchCategory),
ml_gen_info_get_globals(Globals),
{ globals__lookup_bool_option(Globals, smart_indexing, Indexing) },
(
/**************
XXX Lookup switches are NYI
When we do get around to implementing them,
they should probably be handled in ml_simplify_switch rather than here.
{ Indexing = yes },
{ SwitchCategory = atomic_switch },
% Note that if/when the MLDS back-end supports execution
% tracing, we would also need to check that tracing is not
% enabled.
{ list__length(TaggedCases, NumCases) },
{ globals__lookup_int_option(Globals, lookup_switch_size,
LookupSize) },
{ NumCases >= LookupSize },
{ globals__lookup_int_option(Globals, lookup_switch_req_density,
ReqDensity) },
lookup_switch__is_lookup_switch(CaseVar, TaggedCases, GoalInfo,
CanFail, ReqDensity,
CodeModel, FirstVal, LastVal, NeedRangeCheck,
NeedBitVecCheck, OutVars, CaseVals)
->
{ MaybeEnd = MaybeEndPrime },
ml_lookup_switch__generate(CaseVar, OutVars, CaseVals,
FirstVal, LastVal, NeedRangeCheck, NeedBitVecCheck,
MLDS_Decls, MLDS_Statements)
;
**************/
%
% Try using a string hash switch
%
{ Indexing = yes },
{ SwitchCategory = string_switch },
{ list__length(TaggedCases, NumCases) },
{ globals__lookup_int_option(Globals, string_switch_size,
StringSize) },
{ NumCases >= StringSize },
% We can implement string hash switches using either
% computed gotos or int switches.
(
{ target_supports_computed_goto(Globals) }
;
{ target_supports_int_switch(Globals) }
),
% XXX Currently string hash switches always use gotos
% (to break out of the hash chain loop).
% We should change that, so that we can use string hash
% switches for the Java back-end too.
{ target_supports_goto(Globals) },
% OK, we could use a string hash switch. But should we?
% We may prefer to do a direct-mapped string switch.
\+ {
target_supports_string_switch(Globals),
globals__lookup_bool_option(Globals, prefer_switch, yes)
}
->
ml_string_switch__generate(TaggedCases, CaseVar, CodeModel,
CanFail, Context, MLDS_Decls, MLDS_Statements)
;
%
% Try using a tag switch
%
{ Indexing = yes },
{ SwitchCategory = tag_switch },
{ list__length(TaggedCases, NumCases) },
{ globals__lookup_int_option(Globals, tag_switch_size,
TagSize) },
{ NumCases >= TagSize },
{ target_supports_int_switch(Globals) }
->
ml_tag_switch__generate(TaggedCases, CaseVar, CodeModel,
CanFail, Context, MLDS_Decls, MLDS_Statements)
;
%
% Try using a "direct-mapped" switch.
% This also handles dense (computed goto) switches --
% for those, we first generate a direct-mapped switch,
% and then convert it into a computed goto switch
% in ml_simplify_switch.
%
{ Indexing = yes },
(
{ target_supports_switch(SwitchCategory, Globals) }
;
{ SwitchCategory = atomic_switch },
{ target_supports_computed_goto(Globals) }
)
->
ml_switch_generate_mlds_switch(TaggedCases, CaseVar,
CodeModel, CanFail, Context,
MLDS_Decls, MLDS_Statements)
;
%
% The fallback method: if all else fails, generate an
% if-then-else chain which tests each of the cases in turn.
%
ml_switch_generate_if_else_chain(TaggedCases, CaseVar,
CodeModel, CanFail, Context,
MLDS_Decls, MLDS_Statements)
).
%-----------------------------------------------------------------------------%
:- pred target_supports_switch(switch_category::in, globals::in)
is semidet.
target_supports_switch(SwitchCategory, Globals) :-
(
SwitchCategory = atomic_switch,
target_supports_int_switch(Globals)
;
SwitchCategory = string_switch,
target_supports_string_switch(Globals)
).
target_supports_int_switch(Globals) :-
globals__get_target(Globals, Target),
target_supports_int_switch_2(Target) = yes.
target_supports_string_switch(Globals) :-
globals__get_target(Globals, Target),
target_supports_string_switch_2(Target) = yes.
target_supports_goto(Globals) :-
globals__get_target(Globals, Target),
target_supports_goto_2(Target) = yes.
target_supports_computed_goto(Globals) :-
globals__get_target(Globals, Target),
target_supports_computed_goto_2(Target) = yes.
:- func target_supports_int_switch_2(compilation_target) = bool.
:- func target_supports_string_switch_2(compilation_target) = bool.
:- func target_supports_goto_2(compilation_target) = bool.
:- func target_supports_computed_goto_2(compilation_target) = bool.
target_supports_int_switch_2(c) = yes.
target_supports_int_switch_2(asm) = yes. % asm means via gnu back-end
target_supports_int_switch_2(il) = no.
target_supports_int_switch_2(java) = yes.
% target_supports_int_switch_2(c_sharp) = yes.
target_supports_string_switch_2(c) = no.
target_supports_string_switch_2(asm) = no. % asm means via gnu back-end
target_supports_string_switch_2(il) = no.
target_supports_string_switch_2(java) = no.
% target_supports_string_switch_2(c_sharp) = yes.
target_supports_computed_goto_2(c) = yes.
target_supports_computed_goto_2(asm) = no. % asm means via gnu back-end
% XXX for asm, it should be `yes', but currently
% computed gotos are not yet implemented in gcc.m.
target_supports_computed_goto_2(il) = yes.
target_supports_computed_goto_2(java) = no.
% target_supports_computed_goto_2(c_sharp) = no.
target_supports_goto_2(c) = yes.
target_supports_goto_2(asm) = yes. % asm means via gnu back-end
target_supports_goto_2(il) = yes.
target_supports_goto_2(java) = no.
% target_supports_goto_2(c_sharp) = yes.
%-----------------------------------------------------------------------------%
% We categorize switches according to whether the value
% being switched on is an atomic type, a string, or
% something more complicated.
:- pred ml_switch_gen__determine_category(prog_var, switch_category,
ml_gen_info, ml_gen_info).
:- mode ml_switch_gen__determine_category(in, out, in, out) is det.
ml_switch_gen__determine_category(CaseVar, SwitchCategory) -->
ml_variable_type(CaseVar, Type),
=(MLGenInfo),
{ ml_gen_info_get_module_info(MLGenInfo, ModuleInfo) },
{ type_util__classify_type(Type, ModuleInfo, TypeCategory) },
{ switch_util__type_cat_to_switch_cat(TypeCategory, SwitchCategory) }.
%-----------------------------------------------------------------------------%
% Look up the representation (tag) for the cons_id in each case.
% Also look up the priority of each tag test.
%
:- pred ml_switch_lookup_tags(list(case), prog_var, cases_list,
ml_gen_info, ml_gen_info).
:- mode ml_switch_lookup_tags(in, in, out, in, out) is det.
ml_switch_lookup_tags([], _, []) --> [].
ml_switch_lookup_tags([Case | Cases], Var, [TaggedCase | TaggedCases]) -->
{ Case = case(ConsId, Goal) },
ml_variable_type(Var, Type),
ml_cons_id_to_tag(ConsId, Type, Tag),
{ switch_util__switch_priority(Tag, Priority) },
{ TaggedCase = case(Priority, Tag, ConsId, Goal) },
ml_switch_lookup_tags(Cases, Var, TaggedCases).
%-----------------------------------------------------------------------------%
% Generate a chain of if-then-elses to test each case in turn.
%
:- pred ml_switch_generate_if_else_chain(list(extended_case), prog_var,
code_model, can_fail, prog_context, mlds__defns, mlds__statements,
ml_gen_info, ml_gen_info).
:- mode ml_switch_generate_if_else_chain(in, in, in, in, in, out, out,
in, out) is det.
ml_switch_generate_if_else_chain([], _Var, CodeModel, CanFail, Context,
[], MLDS_Statements) -->
( { CanFail = can_fail } ->
ml_gen_failure(CodeModel, Context, MLDS_Statements)
;
{ error("switch failure") }
).
ml_switch_generate_if_else_chain([Case | Cases], Var, CodeModel, CanFail,
Context, MLDS_Decls, MLDS_Statements) -->
{ Case = case(_, _Tag, ConsId, Goal) },
(
{ Cases = [], CanFail = cannot_fail }
->
ml_gen_goal(CodeModel, Goal, MLDS_Decls, MLDS_Statements)
;
ml_gen_tag_test(Var, ConsId, TagTestDecls, TagTestStatements,
TagTestExpression),
ml_gen_goal(CodeModel, Goal, GoalStatement),
ml_switch_generate_if_else_chain(Cases, Var, CodeModel,
CanFail, Context, RestDecls, RestStatements),
{ Rest = ml_gen_block(RestDecls, RestStatements, Context) },
{ IfStmt = if_then_else(TagTestExpression,
GoalStatement, yes(Rest)) },
{ IfStatement = mlds__statement(IfStmt,
mlds__make_context(Context)) },
{ MLDS_Decls = TagTestDecls },
{ MLDS_Statements = list__append(TagTestStatements,
[IfStatement]) }
).
%-----------------------------------------------------------------------------%
% Generate an MLDS switch.
% This is used for "direct-mapped" switches, where we map a
% Mercury switch directly to a switch in the target language.
%
:- pred ml_switch_generate_mlds_switch(list(extended_case), prog_var,
code_model, can_fail, prog_context, mlds__defns, mlds__statements,
ml_gen_info, ml_gen_info).
:- mode ml_switch_generate_mlds_switch(in, in, in, in, in, out, out,
in, out) is det.
ml_switch_generate_mlds_switch(Cases, Var, CodeModel, CanFail,
Context, MLDS_Decls, MLDS_Statements) -->
ml_variable_type(Var, Type),
ml_gen_type(Type, MLDS_Type),
ml_gen_var(Var, Lval),
{ Rval = mlds__lval(Lval) },
ml_switch_gen_range(MLDS_Type, Range),
ml_switch_generate_mlds_cases(Cases, CodeModel, MLDS_Cases),
ml_switch_generate_default(CanFail, CodeModel, Context, Default),
{ SwitchStmt0 = switch(MLDS_Type, Rval, Range, MLDS_Cases, Default) },
{ MLDS_Context = mlds__make_context(Context) },
ml_simplify_switch(SwitchStmt0, MLDS_Context, SwitchStatement),
{ MLDS_Decls = [] },
{ MLDS_Statements = [SwitchStatement] }.
:- pred ml_switch_gen_range(mlds__type, mlds__switch_range,
ml_gen_info, ml_gen_info).
:- mode ml_switch_gen_range(in, out, in, out) is det.
ml_switch_gen_range(MLDS_Type, Range) -->
=(MLGenInfo),
{
MLDS_Type = mercury_type(Type, TypeCategory),
ml_gen_info_get_module_info(MLGenInfo, ModuleInfo),
switch_util__type_range(TypeCategory, Type, ModuleInfo,
MinRange, MaxRange)
->
Range = range(MinRange, MaxRange)
;
Range = range_unknown
}.
:- pred ml_switch_generate_mlds_cases(list(extended_case),
code_model, list(mlds__switch_case), ml_gen_info, ml_gen_info).
:- mode ml_switch_generate_mlds_cases(in, in, out, in, out) is det.
ml_switch_generate_mlds_cases([], _, []) --> [].
ml_switch_generate_mlds_cases([Case | Cases], CodeModel,
[MLDS_Case | MLDS_Cases]) -->
ml_switch_generate_mlds_case(Case, CodeModel, MLDS_Case),
ml_switch_generate_mlds_cases(Cases, CodeModel, MLDS_Cases).
:- pred ml_switch_generate_mlds_case(extended_case, code_model,
mlds__switch_case, ml_gen_info, ml_gen_info).
:- mode ml_switch_generate_mlds_case(in, in, out, in, out) is det.
ml_switch_generate_mlds_case(Case, CodeModel, MLDS_Case) -->
{ Case = case(_Priority, Tag, _ConsId, Goal) },
( { Tag = int_constant(Int) } ->
{ Rval = const(int_const(Int)) }
; { Tag = string_constant(String) } ->
{ Rval = const(string_const(String)) }
;
{ error("ml_switch_gen.m: invalid tag type") }
),
ml_gen_goal(CodeModel, Goal, MLDS_Statement),
{ MLDS_Case = [match_value(Rval)] - MLDS_Statement }.
% Generate an appropriate default for a switch.
%
ml_switch_generate_default(CanFail, CodeModel, Context, Default) -->
(
{ CanFail = can_fail },
ml_gen_failure(CodeModel, Context, FailStatements),
( { FailStatements = [] } ->
{ Default = default_do_nothing }
;
{ Fail = ml_gen_block([], FailStatements, Context) },
{ Default = default_case(Fail) }
)
;
{ CanFail = cannot_fail },
{ Default = default_is_unreachable }
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
|