1 /*
2  * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_ARM_V2_ARM_HELPERS_H
8 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_ARM_V2_ARM_HELPERS_H
9 
10 /* This file defines arm helper functions that are used by the
11    method definitions in armv7.table. They exists because either
12    (a) ARM specifications use them, or (b) the limitations of
13    (dgen) bit expressions (defined in dgen_input.py) do not allow us
14    to specify the function explicitly.
15 */
16 
17 #include "native_client/src/trusted/validator_arm/model.h"
18 
19 namespace nacl_arm_dec {
20 
21 // Function to get the number of general purpose registers in
22 // a register list.
NumGPRs(RegisterList registers)23 inline uint32_t NumGPRs(RegisterList registers) {
24   return registers.numGPRs();
25 }
26 
27 // Function to return the smallest general purpose register in
28 // a register list.
SmallestGPR(RegisterList registers)29 inline uint32_t SmallestGPR(RegisterList registers) {
30   return registers.SmallestGPR();
31 }
32 
33 // Function that returns true if the general purpose register
34 // in in the register list.
Contains(RegisterList registers,Register r)35 inline bool Contains(RegisterList registers, Register r) {
36   return registers.Contains(r);
37 }
38 
39 // Function that unions together to register lists.
Union(RegisterList r1,RegisterList r2)40 inline RegisterList Union(RegisterList r1, RegisterList r2) {
41   return r1.Union(r2);
42 }
43 
44 // Function returning the register index of a register.
RegIndex(Register r)45 inline Register::Number RegIndex(Register r) {
46   return r.number();
47 }
48 
49 // Function that expands an immediate value, corresponding
50 // to ARMExpandImm, as described in section A5.2.4 of the manual.
ARMExpandImm(uint32_t imm12)51 inline uint32_t ARMExpandImm(uint32_t imm12) {
52   uint32_t unrotated_value = imm12 & 0xFF;
53   uint32_t ror_amount = ((imm12 >> 8) & 0xF) << 1;
54   return (ror_amount == 0) ?
55       unrotated_value :
56       ((unrotated_value >> ror_amount) |
57        (unrotated_value << (32 - ror_amount)));
58 }
59 
60 // Function ARMExpandImm_C, less the carry, as described in
61 // section A5.2.4 of the manual.
ARMExpandImm_C(uint32_t imm12)62 inline uint32_t ARMExpandImm_C(uint32_t imm12) {
63   return ARMExpandImm(imm12);
64 }
65 
66 // Function that expands an imm8 to a corresponding floating point
67 // value with the given (n) number of bits, where n is 32 or 64.
68 uint64_t VFPExpandImm(uint32_t imm8, int n);
69 
70 // Returns the bits used as a literal pool head.
LiteralPoolHeadConstant()71 inline uint32_t LiteralPoolHeadConstant() {
72   return kLiteralPoolHead;
73 }
74 
75 // Returns true if the UDF instruction matches encoding values we've chosen
76 // to be safe.
IsUDFNaClSafe(uint32_t inst_bits)77 inline bool IsUDFNaClSafe(uint32_t inst_bits) {
78   return inst_bits == kHaltFill || inst_bits == kAbortNow;
79 }
80 
81 }  // namespace nacl_arm_dec
82 
83 #endif  // NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_ARM_V2_ARM_HELPERS_H
84