MCPcopy Create free account
hub / github.com/akyoto/q / canEncodeNumber

Method canEncodeNumber

src/codegen/canEncodeNumber.go:12–115  ·  view source on GitHub ↗

canEncodeNumber returns true if the architecture can encode an immediate for the given instruction.

(instr ssa.Value, number *ssa.Int)

Source from the content-addressed store, hash-verified

10
11// canEncodeNumber returns true if the architecture can encode an immediate for the given instruction.
12func (f *Function) canEncodeNumber(instr ssa.Value, number *ssa.Int) bool {
13 switch instr := instr.(type) {
14 case *ssa.BinaryOp:
15 if number != instr.Right {
16 return false
17 }
18
19 switch f.build.Arch {
20 case config.ARM:
21 if instr.Op.IsComparison() {
22 _, encodable := arm.CompareRegisterNumber(0, number.Int)
23 return encodable
24 }
25
26 switch instr.Op {
27 case token.Add:
28 _, encodable := arm.AddRegisterNumber(0, 0, number.Int)
29 return encodable
30 case token.And:
31 _, encodable := arm.AndRegisterNumber(0, 0, number.Int)
32 return encodable
33 case token.Or:
34 _, encodable := arm.OrRegisterNumber(0, 0, number.Int)
35 return encodable
36 case token.Shl, token.Shr:
37 return number.Int >= 0 && number.Int <= 63
38 case token.Sub:
39 _, encodable := arm.SubRegisterNumber(0, 0, number.Int)
40 return encodable
41 case token.Xor:
42 _, encodable := arm.XorRegisterNumber(0, 0, number.Int)
43 return encodable
44 }
45 case config.X86:
46 if instr.Op.IsComparison() {
47 return cpu.SizeInt(number.Int) <= 4
48 }
49
50 switch instr.Op {
51 case token.Add, token.And, token.Or, token.Sub, token.Xor:
52 return cpu.SizeInt(number.Int) <= 4
53 case token.Shl, token.Shr:
54 return number.Int >= 0 && number.Int <= 63
55 }
56 }
57 case *ssa.Load:
58 if instr.Memory.Index != number {
59 return false
60 }
61
62 switch f.build.Arch {
63 case config.ARM:
64 if instr.Memory.Scale {
65 return number.Int >= 0 && number.Int <= 4095
66 } else {
67 return number.Int >= -256 && number.Int <= 255
68 }
69 case config.X86:

Callers 1

needsRegisterMethod · 0.95

Calls 8

CompareRegisterNumberFunction · 0.92
AddRegisterNumberFunction · 0.92
AndRegisterNumberFunction · 0.92
OrRegisterNumberFunction · 0.92
SubRegisterNumberFunction · 0.92
XorRegisterNumberFunction · 0.92
SizeIntFunction · 0.92
IsComparisonMethod · 0.80

Tested by

no test coverage detected