Skip to content

switch statements #428

@gtoal

Description

@gtoal

Sorry, I'm not up to speed on this github pulling lark, but if you don't mind adding it yourself, I think this code will add the switch statement to mir2c.c:

gtoal@hplinux:~/src/mir$ diff mir2c/mir2c.c mir-master/mir2c/mir2c.c
344c344
<     fprintf (f, " = (int64_t)alloca (");
---
>     fprintf (f, " = alloca (");
578,605d577
<   case MIR_SWITCH:
<     /* Using GCC extensions, the missing switch statement could be translated as:
<          goto *(void *[]){&&l5, &&l6, &&l7, &&l8, &&l9, &&l10, &&l11, &&l12}[I_12];
<        or if not GCC then create a switch and have each switch case do 'goto l5;' etc.
<        (that example is from the Duff's Device test)
<      */
<     fprintf(f, "#ifdef __GNUC__\n");
<     fprintf(f, "  goto *(void *[]){");
<     for (int i = 1; i < insn->nops; i++) {
<       fprintf(f, "&&");
<       out_op(ctx, f, insn->ops[i]);
<       if (i < insn->nops-1) fprintf(f, ",");
<     }
<     fprintf(f, "}[");
<     out_op(ctx, f, insn->ops[0]);
<     fprintf(f, "];\n");
<     fprintf(f, "  #else /* not __GNUC__ */\n");
<     fprintf(f, "  switch (");
<     out_op(ctx, f, insn->ops[0]);
<     fprintf(f, ") {\n");
<     for (int i = 1; i < insn->nops; i++) {
<       fprintf(f, "    case %d: goto ", i-1);
<       out_op(ctx, f, insn->ops[i]);
<       fprintf(f, ";\n");
<     }
<     fprintf(f, "  };\n");
<     fprintf(f, "  #endif /* not __GNUC__ */\n");
<     break;
809c781
<   fprintf (f, "#include <stdint.h>\n#include <stdarg.h>\n#include <alloca.h>\n");
---
>   fprintf (f, "#include <stdint.h>\n#include <stdarg.h>\n");

(PS It took me a minute to realise why building mir on my primary linux system gave so many errors - although it builds on an x86_64 architecture, it doesn't compile on an i686 machine. It ought to compile as a cross-compiler even though it's not a supported target architecture. Or at least give a helpful error from the build process.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions