summaryrefslogtreecommitdiff
path: root/src/cmd/9g/prog.c
blob: 51c132d183d1b5172d537e1a0bc1ff2082de80fd (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2014 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include <u.h>
#include <libc.h>
#include "gg.h"
#include "opt.h"

enum {
	LeftRdwr = LeftRead | LeftWrite,
	RightRdwr = RightRead | RightWrite,
};

// This table gives the basic information about instruction
// generated by the compiler and processed in the optimizer.
// See opt.h for bit definitions.
//
// Instructions not generated need not be listed.
// As an exception to that rule, we typically write down all the
// size variants of an operation even if we just use a subset.
//
// The table is formatted for 8-space tabs.
static ProgInfo progtable[ALAST] = {
	[ATYPE]=	{Pseudo | Skip},
	[ATEXT]=	{Pseudo},
	[AFUNCDATA]=	{Pseudo},
	[APCDATA]=	{Pseudo},
	[AUNDEF]=	{Break},
	[AUSEFIELD]=	{OK},
	[ACHECKNIL]=	{LeftRead},
	[AVARDEF]=	{Pseudo | RightWrite},
	[AVARKILL]=	{Pseudo | RightWrite},

	// NOP is an internal no-op that also stands
	// for USED and SET annotations, not the Power opcode.
	[ANOP]=		{LeftRead | RightWrite},

	// Integer
	[AADD]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[ASUB]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[ANEG]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[AAND]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[AOR]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[AXOR]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[AMULLD]=	{SizeQ | LeftRead | RegRead | RightWrite},
	[AMULLW]=	{SizeL | LeftRead | RegRead | RightWrite},
	[AMULHD]=	{SizeL | LeftRead | RegRead | RightWrite},
	[AMULHDU]=	{SizeL | LeftRead | RegRead | RightWrite},
	[ADIVD]=	{SizeQ | LeftRead | RegRead | RightWrite},
	[ADIVDU]=	{SizeQ | LeftRead | RegRead | RightWrite},
	[ASLD]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[ASRD]=		{SizeQ | LeftRead | RegRead | RightWrite},
	[ASRAD]=	{SizeQ | LeftRead | RegRead | RightWrite},
	[ACMP]=		{SizeQ | LeftRead | RightRead},
	[ACMPU]=	{SizeQ | LeftRead | RightRead},
	[ATD]=		{SizeQ | RightRead},

	// Floating point.
	[AFADD]=	{SizeD | LeftRead | RegRead | RightWrite},
	[AFADDS]=	{SizeF | LeftRead | RegRead | RightWrite},
	[AFSUB]=	{SizeD | LeftRead | RegRead | RightWrite},
	[AFSUBS]=	{SizeF | LeftRead | RegRead | RightWrite},
	[AFMUL]=	{SizeD | LeftRead | RegRead | RightWrite},
	[AFMULS]=	{SizeF | LeftRead | RegRead | RightWrite},
	[AFDIV]=	{SizeD | LeftRead | RegRead | RightWrite},
	[AFDIVS]=	{SizeF | LeftRead | RegRead | RightWrite},
	[AFCTIDZ]=	{SizeF | LeftRead | RegRead | RightWrite},
	[AFCFID]=	{SizeF | LeftRead | RegRead | RightWrite},
	[AFCMPU]=	{SizeD | LeftRead | RightRead},
	[AFRSP]=	{SizeD | LeftRead | RightWrite | Conv},

	// Moves
	[AMOVB]=	{SizeB | LeftRead | RightWrite | Move | Conv},
	[AMOVBU]=	{SizeB | LeftRead | RightWrite | Move | Conv | PostInc},
	[AMOVBZ]=	{SizeB | LeftRead | RightWrite | Move | Conv},
	[AMOVH]=	{SizeW | LeftRead | RightWrite | Move | Conv},
	[AMOVHU]=	{SizeW | LeftRead | RightWrite | Move | Conv | PostInc},
	[AMOVHZ]=	{SizeW | LeftRead | RightWrite | Move | Conv},
	[AMOVW]=	{SizeL | LeftRead | RightWrite | Move | Conv},
	// there is no AMOVWU.
	[AMOVWZU]=	{SizeL | LeftRead | RightWrite | Move | Conv | PostInc},
	[AMOVWZ]=	{SizeL | LeftRead | RightWrite | Move | Conv},
	[AMOVD]=	{SizeQ | LeftRead | RightWrite | Move},
	[AMOVDU]=	{SizeQ | LeftRead | RightWrite | Move | PostInc},
	[AFMOVS]=	{SizeF | LeftRead | RightWrite | Move | Conv},
	[AFMOVD]=	{SizeD | LeftRead | RightWrite | Move},

	// Jumps
	[ABR]=		{Jump | Break},
	[ABL]=		{Call},
	[ABEQ]=		{Cjmp},
	[ABNE]=		{Cjmp},
	[ABGE]=		{Cjmp},
	[ABLT]=		{Cjmp},
	[ABGT]=		{Cjmp},
	[ABLE]=		{Cjmp},
	[ARETURN]=	{Break},

	[ADUFFZERO]=	{Call},
	[ADUFFCOPY]=	{Call},
};

void
proginfo(ProgInfo *info, Prog *p)
{
	*info = progtable[p->as];
	if(info->flags == 0) {
		*info = progtable[AADD];
		fatal("proginfo: unknown instruction %P", p);
	}

	if((info->flags & RegRead) && p->reg == NREG) {
		info->flags &= ~RegRead;
		info->flags |= /*CanRegRead |*/ RightRead;
	}

	if((p->from.type == D_OREG || p->from.type == D_CONST) && p->from.reg != NREG) {
		info->regindex |= RtoB(p->from.reg);
		if(info->flags & PostInc) {
			info->regset |= RtoB(p->from.reg);
		}
	}
	if((p->to.type == D_OREG || p->to.type == D_CONST) && p->to.reg != NREG) {
		info->regindex |= RtoB(p->to.reg);
		if(info->flags & PostInc) {
			info->regset |= RtoB(p->to.reg);
		}
	}

	if(p->from.type == D_CONST && p->from.sym != nil && (info->flags & LeftRead)) {
		info->flags &= ~LeftRead;
		info->flags |= LeftAddr;
	}

	if(p->as == ADUFFZERO) {
		info->reguse |= (1<<D_R0) | RtoB(3);
		info->regset |= RtoB(3);
	}
	if(p->as == ADUFFCOPY) {
		// TODO(austin) Revisit when duffcopy is implemented
		info->reguse |= RtoB(3) | RtoB(4) | RtoB(5);
		info->regset |= RtoB(3) | RtoB(4);
	}
}