• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The ChromiumOS Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Architecture-specific information."""
6
7import collections
8import json
9
10
11class Arch(
12    collections.namedtuple(
13        "Arch",
14        [
15            "arch_nr",
16            "arch_name",
17            "bits",
18            "syscalls",
19            "constants",
20            "syscall_groups",
21        ],
22    )
23):
24    """Holds architecture-specific information."""
25
26    def truncate_word(self, value):
27        """Return the value truncated to fit in a word."""
28        return value & self.max_unsigned
29
30    @property
31    def min_signed(self):
32        """The smallest signed value that can be represented in a word."""
33        return -(1 << (self.bits - 1))
34
35    @property
36    def max_unsigned(self):
37        """The largest unsigned value that can be represented in a word."""
38        return (1 << self.bits) - 1
39
40    @staticmethod
41    def load_from_json(json_path):
42        """Return an Arch from a .json file."""
43        with open(json_path, "rb") as json_file:
44            return Arch.load_from_json_bytes(json_file.read())
45
46    @staticmethod
47    def load_from_json_bytes(json_bytes):
48        """Return an Arch from a json string."""
49        constants = json.loads(json_bytes)
50        return Arch(
51            arch_nr=constants["arch_nr"],
52            arch_name=constants["arch_name"],
53            bits=constants["bits"],
54            syscalls=constants["syscalls"],
55            constants=constants["constants"],
56            syscall_groups=constants.get("syscall_groups", {}),
57        )
58