build a machine instruction by itself

I want to have some functions that create machine instructions, not specifying which machine function or basic block or iterator they are part of. And then I want to use that result when adding that instruction to a basic block.

I'm pretty sure you can do this but we have not done this in the Mips port so far. We just use instruction builder.

Anyone know how to do this best, or can point me to some code where this is done?

Tia.

Reed

I want to have some functions that create machine instructions, not specifying which machine function or basic block or iterator they are part of.

All machine instructions must be created by a machine function. It provides the context for memory allocation etc.

And then I want to use that result when adding that instruction to a basic block.

I'm pretty sure you can do this but we have not done this in the Mips port so far. We just use instruction builder.

Anyone know how to do this best, or can point me to some code where this is done?

Did you look at BuildMI?

/jakob

I want to have some functions that create machine instructions, not specifying which machine function or basic block or iterator they are part of.

All machine instructions must be created by a machine function. It provides the context for memory allocation etc.

And then I want to use that result when adding that instruction to a basic block.

I'm pretty sure you can do this but we have not done this in the Mips port so far. We just use instruction builder.

Anyone know how to do this best, or can point me to some code where this is done?

Did you look at BuildMI?

For example, what I did already was create the folllowing:

   void BuildAddiuSpImm(MachineBasicBlock &MBB,
                            MachineBasicBlock::iterator II, DebugLoc DL,
                            int64_t Imm) const;

this will insert one of two Addiu SP, Immediate instruction forms at the point of the iterator,
based on the value of the Immediate field. It does this by using BuildMI.

Probably I should have returned a MachineInstructionBuilder.

I would like to use the function I create in various contexts but just have one base function, that is used to build a proper Addiu Sp, Immediate field. There are various BuildMi forms.

To me, creating a MachineInstr is not related this context of how you are placing it.

So I guess I could use:

   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
   /// of `new MachineInstr'.
   ///
   MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID,
                                    DebugLoc DL,
                                    bool NoImp = false);

so my creator function needs to at least have a parameter of type MachineFunction
I can get the MachineFunction from any MachineBasicBlock using getParent

   MachineInstr BuildAddiuSpImm(MachineFunction &MF, int64_t Imm) const;

I did not want to have to pass any more arguments than Imm.

I ended up settling on a scheme which I'm not completely happy with but it's the best I can see to do.

   const MCInstrDesc& AddiuSpImm(int64_t Imm) const;

and then

       if (isInt<16>(-Remainder))
         BuildMI(MBB, I, DL, AddiuSpImm(-Remainder)).addImm(-Remainder);

So the AddiuSpImm choses which instruction description to use depending on the immediate value and then I'm adding rest of the instruction.

I think that probably I could make a template class based on MachineInstrBuilder but it's more work than I have time to do right now.

I don't like the fact that I'm repeating -Remainder. If I make a single builder function then it does not have that problem but them I'm restricted to having that one builder function.

Maybe I should just keep the builder function I have an layer it on this new one; and just add additional builder functions as needed.

Hi Reed,

You may find how ARM does this a useful reference. It's not the prettiest code in the world, but it is roughly analogous to the problem you're trying to solve, so it may be useful. In particular, have a look at emitT2RegPlusImmediate() and emitARMRegPlusImmediate() and the helper functions they call.

-Jim