Python 3 compat.

Try to keep our Python sources compatible with both Python 2.7 and 3.

Check with 'pylint --py3k' and 'python -3'.
This commit is contained in:
Jakob Stoklund Olesen
2016-08-23 15:16:40 -07:00
parent cab356bd9f
commit 1e1baec50a
20 changed files with 40 additions and 34 deletions

View File

@@ -2,6 +2,7 @@
#
# This script is run from src/libcretonne/build.rs to generate Rust files.
from __future__ import absolute_import
import argparse
import isa
import gen_instr

View File

@@ -5,6 +5,7 @@ The `constant_hash` module can generate constant pre-populated hash tables. We
don't attempt parfect hashing, but simply generate an open addressed
quadratically probed hash table.
"""
from __future__ import absolute_import
def simple_hash(s):

View File

@@ -4,7 +4,7 @@ Cretonne meta language module.
This module provides classes and functions used to describe Cretonne
instructions.
"""
from __future__ import absolute_import
import re
import importlib
from collections import namedtuple
@@ -156,7 +156,7 @@ class SettingGroup(object):
.format(self, SettingGroup._current))
SettingGroup._current = None
if globs:
from predicates import Predicate
from .predicates import Predicate
for name, obj in globs.iteritems():
if isinstance(obj, Setting):
assert obj.name is None, obj.name
@@ -365,7 +365,7 @@ class IntType(ScalarType):
assert bits > 0, 'IntType must have positive number of bits'
super(IntType, self).__init__(
name='i{:d}'.format(bits),
membytes=bits/8,
membytes=bits // 8,
doc="An integer type with {} bits.".format(bits))
self.bits = bits
@@ -379,7 +379,7 @@ class FloatType(ScalarType):
def __init__(self, bits, doc):
assert bits > 0, 'FloatType must have positive number of bits'
super(FloatType, self).__init__(name='f{:d}'.format(bits),
membytes=bits/8, doc=doc)
membytes=bits // 8, doc=doc)
self.bits = bits
def __repr__(self):
@@ -393,7 +393,7 @@ class BoolType(ScalarType):
assert bits > 0, 'BoolType must have positive number of bits'
super(BoolType, self).__init__(
name='b{:d}'.format(bits),
membytes=bits/8,
membytes=bits // 8,
doc="A boolean type with {} bits.".format(bits))
self.bits = bits
@@ -845,7 +845,7 @@ class BoundInstruction(object):
assert len(typevars) <= 1 + len(inst.other_typevars)
def __str__(self):
return '.'.join([self.inst.name, ] + map(str, self.typevars))
return '.'.join([self.inst.name, ] + list(map(str, self.typevars)))
def bind(self, *args):
"""

View File

@@ -4,10 +4,11 @@ Cretonne base instruction set.
This module defines the basic Cretonne instruction set that all targets
support.
"""
from __future__ import absolute_import
from . import TypeVar, Operand, Instruction, InstructionGroup, variable_args
from types import i8, f32, f64
from immediates import imm64, uimm8, ieee32, ieee64, immvector, intcc, floatcc
import entities
from .types import i8, f32, f64
from .immediates import imm64, uimm8, ieee32, ieee64, immvector, intcc, floatcc
from . import entities
instructions = InstructionGroup("base", "Shared base instruction set")

View File

@@ -3,7 +3,7 @@ The `cretonne.entities` module predefines all the Cretonne entity reference
operand types. Thee are corresponding definitions in the `cretonne.entities`
Rust module.
"""
from __future__ import absolute_import
from . import EntityRefKind

View File

@@ -5,11 +5,10 @@ Every instruction format has a corresponding `InstructionData` variant in the
Rust representation of cretonne IL, so all instruction formats must be defined
in this module.
"""
from __future__ import absolute_import
from . import InstructionFormat, value, variable_args
from immediates import imm64, uimm8, ieee32, ieee64, immvector, intcc, floatcc
from entities import ebb, function, jump_table
from .immediates import imm64, uimm8, ieee32, ieee64, immvector, intcc, floatcc
from .entities import ebb, function, jump_table
Nullary = InstructionFormat()

View File

@@ -2,7 +2,7 @@
The `cretonne.immediates` module predefines all the Cretonne immediate operand
types.
"""
from __future__ import absolute_import
from . import ImmediateKind
#: A 64-bit immediate integer operand.

View File

@@ -21,6 +21,8 @@ All predicates have a *context* which determines where they can be evaluated.
For an ISA predicate, the context is the ISA settings group. For an instruction
predicate, the context is the instruction format.
"""
from __future__ import absolute_import
from functools import reduce
def _is_parent(a, b):

View File

@@ -3,7 +3,7 @@ Cretonne shared settings.
This module defines settings are are relevant for all code generators.
"""
from __future__ import absolute_import
from . import SettingGroup, BoolSetting, EnumSetting
group = SettingGroup('shared')

View File

@@ -1,7 +1,7 @@
"""
The cretonne.types module predefines all the Cretonne scalar types.
"""
from __future__ import absolute_import
from . import ScalarType, IntType, FloatType, BoolType
#: Boolean.

View File

@@ -12,7 +12,7 @@ If the build script outputs lines of the form:
cargo will rerun the build script when those files have changed since the last
build.
"""
from __future__ import absolute_import, print_function
import os
from os.path import dirname, abspath, join
@@ -30,7 +30,7 @@ def source_files(top):
def generate():
print "Dependencies from meta language directory:"
print("Dependencies from meta language directory:")
meta = dirname(abspath(__file__))
for path in source_files(meta):
print "cargo:rerun-if-changed=" + path
print("cargo:rerun-if-changed=" + path)

View File

@@ -1,7 +1,7 @@
"""
Generate sources with instruction info.
"""
from __future__ import absolute_import
import srcgen
import constant_hash
from unique_table import UniqueTable, UniqueSeqTable

View File

@@ -1,7 +1,7 @@
"""
Generate sources with settings.
"""
from __future__ import absolute_import
import srcgen
from unique_table import UniqueSeqTable
import constant_hash
@@ -102,7 +102,7 @@ def gen_pred_getter(pred, fmt):
"""
Emit a getter for a pre-computed predicate.
"""
fmt.doc_comment('Computed predicate `{}`.'.format(pred.rust_predicate(0)));
fmt.doc_comment('Computed predicate `{}`.'.format(pred.rust_predicate(0)))
proto = 'pub fn {}(&self) -> bool'.format(pred.name)
with fmt.indented(proto + ' {', '}'):
fmt.line('(self.bytes[{}] & (1 << {})) != 0'.format(

View File

@@ -5,7 +5,7 @@ Cretonne target ISA definitions
The :py:mod:`isa` package contains sub-packages for each target instruction set
architecture supported by Cretonne.
"""
from __future__ import absolute_import
from . import riscv

View File

@@ -24,10 +24,10 @@ RV32G / RV64G
F, and D instruction sets listed above.
"""
import defs
import encodings
import settings
from __future__ import absolute_import
from . import defs
from . import encodings
from . import settings
# Re-export the primary target ISA definition.
isa = defs.isa

View File

@@ -3,7 +3,7 @@ RISC-V definitions.
Commonly used definitions.
"""
from __future__ import absolute_import
from cretonne import TargetISA, CPUMode
import cretonne.base

View File

@@ -1,9 +1,10 @@
"""
RISC-V Encodings.
"""
from __future__ import absolute_import
from cretonne import base
from defs import RV32, RV64
from recipes import OPIMM, OPIMM32, OP, OP32, R, Rshamt
from .defs import RV32, RV64
from .recipes import OPIMM, OPIMM32, OP, OP32, R, Rshamt
# Basic arithmetic binary instructions are encoded in an R-type instruction.
for inst, f3, f7 in [

View File

@@ -8,6 +8,7 @@ instruction formats described in the reference:
Volume I: User-Level ISA
Version 2.1
"""
from __future__ import absolute_import
from cretonne import EncRecipe
from cretonne.formats import Binary, BinaryImm

View File

@@ -1,11 +1,11 @@
"""
RISC-V settings.
"""
from __future__ import absolute_import
from cretonne import SettingGroup, BoolSetting
from cretonne.predicates import And
import cretonne.settings as shared
from defs import isa
from .defs import isa
isa.settings = SettingGroup('riscv', parent=shared.group)

View File

@@ -5,7 +5,7 @@ The `srcgen` module contains generic helper routines and classes for generating
source code.
"""
from __future__ import absolute_import
import sys
import os
import re