1*4b9c6d91SCole Faust#!/usr/bin/env python3 2*4b9c6d91SCole Faust# -*- coding: utf-8 -*- 3*4b9c6d91SCole Faust# 4*4b9c6d91SCole Faust# Copyright (C) 2018 The Android Open Source Project 5*4b9c6d91SCole Faust# 6*4b9c6d91SCole Faust# Licensed under the Apache License, Version 2.0 (the "License"); 7*4b9c6d91SCole Faust# you may not use this file except in compliance with the License. 8*4b9c6d91SCole Faust# You may obtain a copy of the License at 9*4b9c6d91SCole Faust# 10*4b9c6d91SCole Faust# http://www.apache.org/licenses/LICENSE-2.0 11*4b9c6d91SCole Faust# 12*4b9c6d91SCole Faust# Unless required by applicable law or agreed to in writing, software 13*4b9c6d91SCole Faust# distributed under the License is distributed on an "AS IS" BASIS, 14*4b9c6d91SCole Faust# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15*4b9c6d91SCole Faust# See the License for the specific language governing permissions and 16*4b9c6d91SCole Faust# limitations under the License. 17*4b9c6d91SCole Faust"""Architecture-specific information.""" 18*4b9c6d91SCole Faust 19*4b9c6d91SCole Faustimport collections 20*4b9c6d91SCole Faustimport json 21*4b9c6d91SCole Faust 22*4b9c6d91SCole Faust 23*4b9c6d91SCole Faustclass Arch( 24*4b9c6d91SCole Faust collections.namedtuple('Arch', [ 25*4b9c6d91SCole Faust 'arch_nr', 'arch_name', 'bits', 'syscalls', 'constants', 26*4b9c6d91SCole Faust 'syscall_groups' 27*4b9c6d91SCole Faust ])): 28*4b9c6d91SCole Faust """Holds architecture-specific information.""" 29*4b9c6d91SCole Faust 30*4b9c6d91SCole Faust def truncate_word(self, value): 31*4b9c6d91SCole Faust """Return the value truncated to fit in a word.""" 32*4b9c6d91SCole Faust return value & self.max_unsigned 33*4b9c6d91SCole Faust 34*4b9c6d91SCole Faust @property 35*4b9c6d91SCole Faust def min_signed(self): 36*4b9c6d91SCole Faust """The smallest signed value that can be represented in a word.""" 37*4b9c6d91SCole Faust return -(1 << (self.bits - 1)) 38*4b9c6d91SCole Faust 39*4b9c6d91SCole Faust @property 40*4b9c6d91SCole Faust def max_unsigned(self): 41*4b9c6d91SCole Faust """The largest unsigned value that can be represented in a word.""" 42*4b9c6d91SCole Faust return (1 << self.bits) - 1 43*4b9c6d91SCole Faust 44*4b9c6d91SCole Faust @staticmethod 45*4b9c6d91SCole Faust def load_from_json(json_path): 46*4b9c6d91SCole Faust """Return an Arch from a .json file.""" 47*4b9c6d91SCole Faust with open(json_path, 'r') as json_file: 48*4b9c6d91SCole Faust return Arch.load_from_json_bytes(json_file.read()) 49*4b9c6d91SCole Faust 50*4b9c6d91SCole Faust @staticmethod 51*4b9c6d91SCole Faust def load_from_json_bytes(json_bytes): 52*4b9c6d91SCole Faust """Return an Arch from a json string.""" 53*4b9c6d91SCole Faust constants = json.loads(json_bytes) 54*4b9c6d91SCole Faust return Arch( 55*4b9c6d91SCole Faust arch_nr=constants["arch_nr"], 56*4b9c6d91SCole Faust arch_name=constants["arch_name"], 57*4b9c6d91SCole Faust bits=constants["bits"], 58*4b9c6d91SCole Faust syscalls=constants["syscalls"], 59*4b9c6d91SCole Faust constants=constants["constants"], 60*4b9c6d91SCole Faust syscall_groups=constants.get("syscall_groups", {}), 61*4b9c6d91SCole Faust ) 62