aboutsummaryrefslogtreecommitdiff
path: root/type.go
blob: d677472cc8c9fcab4dc821747defaf2f6d3b10a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later

package main

import "fmt"

type cerType interface {
	fmt.Stringer
	qbeBaseType() string
	qbeExtType() string
	qbeABIType() string
}

type cerInteger struct {
	bits     int
	unsigned bool
}

func (c cerInteger) String() string {
	unsigned := ""
	if c.unsigned {
		unsigned = "u"
	}

	return fmt.Sprintf("%sint%d", unsigned, c.bits)
}

func (c cerInteger) qbeBaseType() string {
	switch {
	case c.bits > 32:
		return "l"
	default:
		return "w"
	}
}

func (c cerInteger) qbeExtType() string {
	switch {
	case c.bits > 32:
		return "l"
	case c.bits > 16:
		return "w"
	case c.bits > 8:
		return "h"
	default:
		return "b"
	}
}

func (c cerInteger) qbeABIType() string {
	signedness := "s"
	if c.unsigned {
		signedness = "u"
	}

	switch {
	case c.bits > 32:
		return "l"
	case c.bits > 16:
		return "w"
	case c.bits > 8:
		return signedness + "h"
	default:
		return signedness + "b"
	}
}

func resolveType(name string) cerType {
	switch name {
	case "int8":
		return cerInteger{bits: 8}
	case "uint8":
		return cerInteger{bits: 8, unsigned: true}
	case "int16":
		return cerInteger{bits: 16}
	case "uint16":
		return cerInteger{bits: 16, unsigned: true}
	case "int32":
		return cerInteger{bits: 32}
	case "uint32":
		return cerInteger{bits: 32, unsigned: true}
	case "int64":
		return cerInteger{bits: 64}
	case "uint64":
		return cerInteger{bits: 64, unsigned: true}
	default:
		return nil
	}
}

var (
	cerInt8   = cerInteger{bits: 8}
	cerUint8  = cerInteger{bits: 8, unsigned: true}
	cerInt16  = cerInteger{bits: 16}
	cerUint16 = cerInteger{bits: 16, unsigned: true}
	cerInt32  = cerInteger{bits: 32}
	cerUint32 = cerInteger{bits: 32, unsigned: true}
	cerInt64  = cerInteger{bits: 64}
	cerUint64 = cerInteger{bits: 64, unsigned: true}
)