Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Release 1.0
[palacios.git] / misc / decoder_test / XED2 / examples / xed-ex3.cpp
1 /*BEGIN_LEGAL 
2 Intel Open Source License 
3
4 Copyright (c) 2002-2007 Intel Corporation 
5 All rights reserved. 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9
10 Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.  Redistributions
12 in binary form must reproduce the above copyright notice, this list of
13 conditions and the following disclaimer in the documentation and/or
14 other materials provided with the distribution.  Neither the name of
15 the Intel Corporation nor the names of its contributors may be used to
16 endorse or promote products derived from this software without
17 specific prior written permission.
18  
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
23 ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 END_LEGAL */
31 /// @file xed-ex3.cpp
32 /// @author Mark Charney   <mark.charney@intel.com>
33
34 /// Encoder example
35
36
37 extern "C" {
38 #include "xed-interface.h"
39 #include "xed-examples-util.h"
40 }
41 #include "xed-enc-lang.H"
42 #include <iostream>
43 #include <iomanip>
44 #include <string>
45 #include <sstream>
46 using namespace std;
47
48 int main(int argc, char** argv);
49
50
51 void
52 usage(char* progname)
53 {
54     cerr << "Usage: " << progname << " [-16|-32|-64] [-a16|-a32] [-s16|-s32] encode-string" << endl;
55     exit(1);
56 }
57
58 ascii_encode_request_t
59 parse_args(unsigned int argc, char** argv)
60 {
61     if (argc == 1)
62         usage(argv[0]);
63
64     ascii_encode_request_t r;
65     r.dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
66     r.dstate.addr_width = XED_ADDRESS_WIDTH_32b;
67     r.dstate.stack_addr_width = XED_ADDRESS_WIDTH_32b;
68     string c = "";
69     unsigned int i = 1;
70     
71     for( ; i< argc; i++)
72         if (strcmp(argv[i],"-16")==0) 
73             r.dstate.mmode = XED_MACHINE_MODE_LEGACY_16;
74         else if (strcmp(argv[i],"-32")==0) 
75             r.dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
76         else if (strcmp("-64", argv[i]) == 0) {
77             r.dstate.mmode = XED_MACHINE_MODE_LONG_64;
78             r.dstate.addr_width = XED_ADDRESS_WIDTH_64b;
79         }
80         else if (strcmp("-32", argv[i]) == 0)
81             r.dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
82         else if (strcmp("-16", argv[i]) == 0)
83             r.dstate.mmode = XED_MACHINE_MODE_LEGACY_16;
84         else if (strcmp(argv[i],"-a32")==0) 
85             r.dstate.addr_width = XED_ADDRESS_WIDTH_32b;
86         else if (strcmp(argv[i],"-a16")==0) 
87             r.dstate.addr_width = XED_ADDRESS_WIDTH_16b;
88         else if (strcmp(argv[i],"-s32")==0) 
89             r.dstate.stack_addr_width = XED_ADDRESS_WIDTH_32b;
90         else if (strcmp(argv[i],"-s16")==0) 
91             r.dstate.stack_addr_width = XED_ADDRESS_WIDTH_16b;
92         else
93             break;
94
95     if (i == argc)
96         usage(argv[0]);
97
98     for( ;i<argc;i++)
99         c = c + " " + argv[i];
100
101     r.command = c.c_str();
102     return r;
103 }
104
105 int main(int argc, char** argv) {
106     char buf[5000];
107     xed_tables_init();
108     ascii_encode_request_t areq = parse_args(argc,argv);
109     xed_encoder_request_t req = parse_encode_request(areq);
110     
111     cout << "Encode request:" << endl;
112     xed_encode_request_print(&req, buf, 5000);
113     cout << buf << endl;
114     
115     unsigned int ilen = XED_MAX_INSTRUCTION_BYTES;
116     unsigned int olen;
117     xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES];
118
119     xed_error_enum_t xed_error =  xed_encode(&req, array, ilen, &olen);
120     xed_bool_t encode_okay = ( xed_error == XED_ERROR_NONE);
121     if (!encode_okay)     {
122         cout << "Could not encode" << endl;
123         return 1;
124     }
125     xed_print_hex_line(buf, array, olen);
126     cout << "Encodable! " << buf << endl;
127
128 //#define TEST_DISPLACEMENT_MODIFICATION
129 //#define TEST_IMMEDIATE_MODIFICATION
130 #if defined(TEST_DISPLACEMENT_MODIFICATION) || defined(TEST_IMMEDIATE_MODIFICATION)
131 # if defined(TEST_DISPLACEMENT_MODIFICATION)
132     if (req.has_disp())
133     {
134         INT64 new_disp = 0x11223344;
135         unsigned int new_disp_length = 4;
136         xed_bool_t update_okay = req.update_displacement(new_disp, new_disp_length, array);
137 # elif defined(TEST_IMMEDIATE_MODIFICATION)
138     if (req.has_immed())
139     {
140         INT64 new_immed = 0x11223344;
141         unsigned int new_immed_length = 4;
142         xed_bool_t update_okay = req.update_immediate(new_immed, new_immed_length, array);
143 # endif
144         if (update_okay)
145         {
146             cout << "Update succeeded" << endl;
147             ostringstream os;
148             print_hex_line(os, array, olen);
149             cout << os.str() << endl;
150         }
151         else
152             cout << "Update failed" << endl;
153     }
154 #endif
155     return 0;
156 }