Exemple #1
0
func TestGlobal(t *testing.T) {
	golden := []struct {
		s    string
		want string
	}{
		// i=0
		{s: "foo", want: "@foo"},
		// i=1
		{s: "a b", want: `@a\20b`},
		// i=2
		{s: "$a", want: "@$a"},
		// i=3
		{s: "-a", want: "@-a"},
		// i=4
		{s: ".a", want: "@.a"},
		// i=5
		{s: "_a", want: "@_a"},
		// i=6
		{s: "#a", want: `@\23a`},
		// i=7
		{s: "a b#c", want: `@a\20b\23c`},
		// i=8
		{s: "2", want: "@2"},
		// i=9
		{s: "foo世bar", want: `@foo\E4\B8\96bar`},
	}

	for i, g := range golden {
		got := enc.Global(g.s)
		if got != g.want {
			t.Errorf("i=%d: name mismatch; expected %q, got %q", i, g.want, got)
		}
	}
}
Exemple #2
0
// String returns the string representation of the global variable declaration.
func (d *GlobalDecl) String() string {
	immutableSpec := "global"
	if d.Immutable() {
		immutableSpec = "constant"
	}

	// External global variable declaration; e.g.
	//    @y = external global i32
	if d.val == nil {
		return fmt.Sprintf("%s = external %s %s", enc.Global(d.Name()), immutableSpec, d.Underlying())
	}

	// Global variable definition; e.g.
	//     @x = global i32 42
	//     @s = constant [13 x i8] c"hello world\0A\00"
	return fmt.Sprintf("%s = %s %s %s", enc.Global(d.Name()), immutableSpec, d.Underlying(), d.Value().ValueString())
}
Exemple #3
0
// String returns the string representation of the instruction.
func (inst *Call) String() string {
	argsBuf := new(bytes.Buffer)
	for i, arg := range inst.args {
		if i > 0 {
			argsBuf.WriteString(", ")
		}
		fmt.Fprintf(argsBuf, "%s %s", arg.Type(), arg.ValueString())
	}
	// TODO: Use inst.callee.ValueString() rather than enc.Global(inst.callee),
	// once the type of callee is changed to NamedValue or *ir.Function.
	return fmt.Sprintf("call %s %s(%s)", inst.result, enc.Global(inst.callee), argsBuf)
}
Exemple #4
0
func BenchmarkGlobalReplace(b *testing.B) {
	for i := 0; i < b.N; i++ {
		enc.Global("$foo bar#baz")
	}
}
Exemple #5
0
// ValueString returns a string representation of the value.
func (d *GlobalDecl) ValueString() string {
	return enc.Global(d.Name())
}
Exemple #6
0
// ValueString returns a string representation of the value.
func (f *Function) ValueString() string {
	return enc.Global(f.Name())
}
Exemple #7
0
// String returns a string representation of the pointer constant; e.g.
//
//    @printf
func (v *Pointer) String() string {
	return enc.Global(v.name)
}