aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder/helpers.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2021-01-09 00:57:55 -0800
committerMatthew Dempsky <mdempsky@google.com>2021-01-14 02:07:48 +0000
commitef5285fbd0636965d916c81dbf87834731f337b2 (patch)
tree005d62b2e4f8bb7373b93d467fb749696d23ef66 /src/cmd/compile/internal/noder/helpers.go
parentf065ff221b546c9ac550d947f89eb3b44b11fc2f (diff)
downloadgo-ef5285fbd0636965d916c81dbf87834731f337b2.tar.gz
go-ef5285fbd0636965d916c81dbf87834731f337b2.zip
[dev.typeparams] cmd/compile: add types2-based noder
This CL adds "irgen", a new noding implementation that utilizes types2 to guide IR construction. Notably, it completely skips dealing with constant and type expressions (aside from using ir.TypeNode to interoperate with the types1 typechecker), because types2 already handled those. It also omits any syntax checking, trusting that types2 already rejected any errors. It currently still utilizes the types1 typechecker for the desugaring operations it handles (e.g., turning OAS2 into OAS2FUNC/etc, inserting implicit conversions, rewriting f(g()) functions, and so on). However, the IR is constructed in a fully incremental fashion, so it should be easy to now piecemeal replace those dependencies as needed. Nearly all of "go test std cmd" passes with -G=3 enabled by default. The main remaining blocker is the number of test/run.go failures. There also appear to be cases where types2 does not provide us with position information. These will be iterated upon. Portions and ideas from Dan Scales's CL 276653. Change-Id: Ic99e8f2d0267b0312d30c10d5d043f5817a59c9d Reviewed-on: https://go-review.googlesource.com/c/go/+/281932 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dan Scales <danscales@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Matthew Dempsky <mdempsky@google.com> Trust: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/noder/helpers.go')
-rw-r--r--src/cmd/compile/internal/noder/helpers.go130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/noder/helpers.go b/src/cmd/compile/internal/noder/helpers.go
new file mode 100644
index 0000000000..2139f16a6c
--- /dev/null
+++ b/src/cmd/compile/internal/noder/helpers.go
@@ -0,0 +1,130 @@
+// Copyright 2021 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.
+
+package noder
+
+import (
+ "cmd/compile/internal/ir"
+ "cmd/compile/internal/typecheck"
+ "cmd/compile/internal/types"
+ "cmd/internal/src"
+ "go/constant"
+)
+
+// Helpers for constructing typed IR nodes.
+//
+// TODO(mdempsky): Move into their own package so they can be easily
+// reused by iimport and frontend optimizations.
+//
+// TODO(mdempsky): Update to consistently return already typechecked
+// results, rather than leaving the caller responsible for using
+// typecheck.Expr or typecheck.Stmt.
+
+// Values
+
+func Const(pos src.XPos, typ *types.Type, val constant.Value) ir.Node {
+ n := ir.NewBasicLit(pos, val)
+ n.SetType(typ)
+ return n
+}
+
+func Nil(pos src.XPos, typ *types.Type) ir.Node {
+ n := ir.NewNilExpr(pos)
+ n.SetType(typ)
+ return n
+}
+
+// Expressions
+
+func Assert(pos src.XPos, x ir.Node, typ *types.Type) ir.Node {
+ return typecheck.Expr(ir.NewTypeAssertExpr(pos, x, ir.TypeNode(typ)))
+}
+
+func Binary(pos src.XPos, op ir.Op, x, y ir.Node) ir.Node {
+ switch op {
+ case ir.OANDAND, ir.OOROR:
+ return ir.NewLogicalExpr(pos, op, x, y)
+ default:
+ return ir.NewBinaryExpr(pos, op, x, y)
+ }
+}
+
+func Call(pos src.XPos, fun ir.Node, args []ir.Node, dots bool) ir.Node {
+ // TODO(mdempsky): This should not be so difficult.
+
+ n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
+ n.IsDDD = dots
+
+ // Actually a type conversion.
+ if fun.Op() == ir.OTYPE {
+ return typecheck.Expr(n)
+ }
+
+ if fun, ok := fun.(*ir.Name); ok && fun.BuiltinOp != 0 {
+ switch fun.BuiltinOp {
+ case ir.OCLOSE, ir.ODELETE, ir.OPANIC, ir.OPRINT, ir.OPRINTN:
+ return typecheck.Stmt(n)
+ default:
+ return typecheck.Expr(n)
+ }
+ }
+
+ // We probably already typechecked fun, and typecheck probably
+ // got it wrong because it didn't know the expression was
+ // going to be called immediately. Correct its mistakes.
+ switch fun := fun.(type) {
+ case *ir.ClosureExpr:
+ fun.Func.SetClosureCalled(true)
+ case *ir.SelectorExpr:
+ if fun.Op() == ir.OCALLPART {
+ op := ir.ODOTMETH
+ if fun.X.Type().IsInterface() {
+ op = ir.ODOTINTER
+ }
+ fun.SetOp(op)
+ fun.SetType(fun.Selection.Type)
+ }
+ }
+
+ typecheck.Call(n)
+ return n
+}
+
+func Compare(pos src.XPos, typ *types.Type, op ir.Op, x, y ir.Node) ir.Node {
+ n := typecheck.Expr(ir.NewBinaryExpr(pos, op, x, y))
+ n.SetType(typ)
+ return n
+}
+
+func Index(pos src.XPos, x, index ir.Node) ir.Node {
+ return ir.NewIndexExpr(pos, x, index)
+}
+
+func Slice(pos src.XPos, x, low, high, max ir.Node) ir.Node {
+ op := ir.OSLICE
+ if max != nil {
+ op = ir.OSLICE3
+ }
+ return ir.NewSliceExpr(pos, op, x, low, high, max)
+}
+
+func Unary(pos src.XPos, op ir.Op, x ir.Node) ir.Node {
+ switch op {
+ case ir.OADDR:
+ return typecheck.NodAddrAt(pos, x)
+ case ir.ODEREF:
+ return ir.NewStarExpr(pos, x)
+ default:
+ return ir.NewUnaryExpr(pos, op, x)
+ }
+}
+
+// Statements
+
+var one = constant.MakeInt64(1)
+
+func IncDec(pos src.XPos, op ir.Op, x ir.Node) ir.Node {
+ x = typecheck.AssignExpr(x)
+ return ir.NewAssignOpStmt(pos, op, x, typecheck.DefaultLit(ir.NewBasicLit(pos, one), x.Type()))
+}