1//===- println.go - IR generation for print and println -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements IR generation for the print and println built-in
10// functions.
11//
12//===----------------------------------------------------------------------===//
13
14package irgen
15
16import (
17	"fmt"
18
19	"llvm.org/llgo/third_party/gotools/go/types"
20)
21
22func (fr *frame) printValues(println_ bool, values ...*govalue) {
23	for i, value := range values {
24		llvm_value := value.value
25
26		typ := value.Type().Underlying()
27		if name, isname := typ.(*types.Named); isname {
28			typ = name.Underlying()
29		}
30
31		if println_ && i > 0 {
32			fr.runtime.printSpace.call(fr)
33		}
34		switch typ := typ.(type) {
35		case *types.Basic:
36			switch typ.Kind() {
37			case types.Uint8, types.Uint16, types.Uint32, types.Uintptr, types.Uint, types.Uint64:
38				i64 := fr.llvmtypes.ctx.Int64Type()
39				zext := fr.builder.CreateZExt(llvm_value, i64, "")
40				fr.runtime.printUint64.call(fr, zext)
41
42			case types.Int, types.Int8, types.Int16, types.Int32, types.Int64:
43				i64 := fr.llvmtypes.ctx.Int64Type()
44				sext := fr.builder.CreateSExt(llvm_value, i64, "")
45				fr.runtime.printInt64.call(fr, sext)
46
47			case types.Float32:
48				llvm_value = fr.builder.CreateFPExt(llvm_value, fr.llvmtypes.ctx.DoubleType(), "")
49				fallthrough
50			case types.Float64:
51				fr.runtime.printDouble.call(fr, llvm_value)
52
53			case types.Complex64:
54				llvm_value = fr.convert(value, types.Typ[types.Complex128]).value
55				fallthrough
56			case types.Complex128:
57				fr.runtime.printComplex.call(fr, llvm_value)
58
59			case types.String, types.UntypedString:
60				fr.runtime.printString.call(fr, llvm_value)
61
62			case types.Bool:
63				fr.runtime.printBool.call(fr, llvm_value)
64
65			case types.UnsafePointer:
66				fr.runtime.printPointer.call(fr, llvm_value)
67
68			default:
69				panic(fmt.Sprint("Unhandled Basic Kind: ", typ.Kind))
70			}
71
72		case *types.Interface:
73			if typ.Empty() {
74				fr.runtime.printEmptyInterface.call(fr, llvm_value)
75			} else {
76				fr.runtime.printInterface.call(fr, llvm_value)
77			}
78
79		case *types.Slice:
80			fr.runtime.printSlice.call(fr, llvm_value)
81
82		case *types.Pointer, *types.Map, *types.Chan, *types.Signature:
83			fr.runtime.printPointer.call(fr, llvm_value)
84
85		default:
86			panic(fmt.Sprintf("Unhandled type kind: %s (%T)", typ, typ))
87		}
88	}
89	if println_ {
90		fr.runtime.printNl.call(fr)
91	}
92}
93