summaryrefslogtreecommitdiffhomepage
path: root/asm/intel64/setcc.cpp
blob: aa7219f12ba02d31d45f0073f6d94280c60c1364 (plain)
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
#include "setcc.h"

#include "codes.h"

#include <asm/assembler.h>
#include <asm/operators.h>

#include <asm/intel64/codes.h>

using namespace std::string_literals;

namespace {
 struct Operation {
  std::string name;
  OP_T opcode;
 };

 std::vector<Operation> setccOps {
  {"seta",  OP_T{ 0x0F, 0x97 }},
  {"setae", OP_T{ 0x0F, 0x93 }},
  {"setb",  OP_T{ 0x0F, 0x92 }},
  {"setbe", OP_T{ 0x0F, 0x96 }},
  {"setc",  OP_T{ 0x0F, 0x92 }},
  {"sete",  OP_T{ 0x0F, 0x94 }},
  {"setg",  OP_T{ 0x0F, 0x9F }},
  {"setge", OP_T{ 0x0F, 0x9D }},
  {"setl",  OP_T{ 0x0F, 0x9C }},
  {"setle", OP_T{ 0x0F, 0x9E }},
  {"setna", OP_T{ 0x0F, 0x96 }},
  {"setnae",OP_T{ 0x0F, 0x92 }},
  {"setnb", OP_T{ 0x0F, 0x93 }},
  {"setnbe",OP_T{ 0x0F, 0x97 }},
  {"setnc", OP_T{ 0x0F, 0x93 }},
  {"setne", OP_T{ 0x0F, 0x95 }},
  {"setng", OP_T{ 0x0F, 0x9E }},
  {"setnge",OP_T{ 0x0F, 0x9C }},
  {"setnl", OP_T{ 0x0F, 0x9D }},
  {"setnle",OP_T{ 0x0F, 0x9F }},
  {"setno", OP_T{ 0x0F, 0x91 }},
  {"setnp", OP_T{ 0x0F, 0x9B }},
  {"setns", OP_T{ 0x0F, 0x99 }},
  {"setnz", OP_T{ 0x0F, 0x95 }},
  {"seto",  OP_T{ 0x0F, 0x90 }},
  {"setp",  OP_T{ 0x0F, 0x9A }},
  {"setpe", OP_T{ 0x0F, 0x9A }},
  {"setpo", OP_T{ 0x0F, 0x9B }},
  {"sets",  OP_T{ 0x0F, 0x98 }},
  {"setz",  OP_T{ 0x0F, 0x94 }},
 };

 bool registerOps() {
  bool result{true};
  for (const auto& setccOp: setccOps) {
   result &= registerOp(mangleName<Asm::Args::Register8>(setccOp.name), [&](const Asm::Args& args) -> std::shared_ptr<Op>{
                        return std::make_shared<Op_setcc>(setccOp.name, args, setccOp.opcode);
                       });
   result &= registerOp(mangleName<Asm::Args::Mem8Ptr64>(setccOp.name), [&](const Asm::Args& args) -> std::shared_ptr<Op>{
                        return std::make_shared<Op_setcc>(setccOp.name, args, setccOp.opcode);
                       });
  }
  return result;
 }

 bool registered {
  registerOps()
 };
}

Op_setcc::Op_setcc(const std::string& name, const Asm::Args& args, const OP_T& opcode)
{
 if (args[0].type() == typeid(Asm::Args::Register8)) { // setcc reg8
  // actually, reg field of ModRM is ignored and could be different from /0
  machine_code = opcode + ModRM("/0", std::any_cast<Asm::Args::Register8>(args[0]).name());

 } else if (args[0].type() == typeid(Asm::Args::Mem8Ptr64)) { // setcc byte ptr [reg64]
  Asm::Args::Mem8Ptr64 ptr{std::any_cast<Asm::Args::Mem8Ptr64>(args[0])};
  // actually, reg field of ModRM is ignored and could be different from /0
  machine_code = opcode + ModRM("/0", ptr.reg(), ptr.offs());

 } else {
  throw std::runtime_error("Unimplemented: setcc "s + args[0].type().name() + " "s + args[1].type().name());
 }
}