[Suif-talk] Name-to-opcode mapping in MachSUIF

Glenn Holloway holloway@deas.harvard.edu
Mon, 17 Dec 2001 14:24:21 -0500 (EST)


David Z Maze writes:

> I'm working on a generic peephole optimizer for Machine SUIF.  The
> user should be able to provide a list of patterns and replacements,
> and the pass will find matching sets of instructions and replace
> them.  In doing this, it would be useful to be able to map from an
> instruction's name to its integer opcode; is there any support in
> MachSUIF to do this?  (Reading the machine library documentation
> didn't suggest so; the best idea I can come up with is to assume that
> the set of supported opcodes is contiguous, and build up a mapping
> using for (opcode = 2; target_implements(opcode); opcode++).)

Yup, that'll do it.  I append a function that fleshes out
this idea.

Glenn
-------------

Map<IdString,int> name_to_opcode;

int
opcode_from_name(const char *name)
{
    static bool map_ready;
    if (!map_ready)
    {
	for (int i = 2; target_implements(i); ++i)
	    name_to_opcode[opcode_name(i)] = i;
	map_ready = true;
    }

    Map<IdString,int>::iterator it = name_to_opcode.find(name);
    claim(it != name_to_opcode.end(), "Not a valid opcode name: '%s'", name);

    return (*it).second;
}