pools_amd64.go raw

   1  /*
   2   * Copyright 2021 ByteDance Inc.
   3   *
   4   * Licensed under the Apache License, Version 2.0 (the "License");
   5   * you may not use this file except in compliance with the License.
   6   * You may obtain a copy of the License at
   7   *
   8   *     http://www.apache.org/licenses/LICENSE-2.0
   9   *
  10   * Unless required by applicable law or agreed to in writing, software
  11   * distributed under the License is distributed on an "AS IS" BASIS,
  12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13   * See the License for the specific language governing permissions and
  14   * limitations under the License.
  15   */
  16  
  17  package encoder
  18  
  19  import (
  20  	"reflect"
  21  	"unsafe"
  22  
  23  	"github.com/bytedance/sonic/internal/encoder/vars"
  24  	"github.com/bytedance/sonic/internal/encoder/x86"
  25  	"github.com/bytedance/sonic/internal/rt"
  26  	"github.com/bytedance/sonic/option"
  27  )
  28  
  29  
  30  func ForceUseJit() {
  31  	x86.SetCompiler(makeEncoderX86)
  32  	pretouchType = pretouchTypeX86
  33  	encodeTypedPointer = x86.EncodeTypedPointer
  34  	vars.UseVM = false
  35  }
  36  
  37  func init() {
  38  	if vars.UseVM {
  39  		ForceUseVM()
  40  	} else {
  41  		ForceUseJit()
  42  	}
  43  }
  44  
  45  var _KeepAlive struct {
  46  	rb    *[]byte
  47  	vp    unsafe.Pointer
  48  	sb    *vars.Stack
  49  	fv    uint64
  50  	err   error
  51  	frame [x86.FP_offs]byte
  52  }
  53  
  54  func makeEncoderX86(vt *rt.GoType, ex ...interface{}) (interface{}, error) {
  55  	pp, err := NewCompiler().Compile(vt.Pack(), ex[0].(bool))
  56  	if err != nil {
  57  		return nil, err
  58  	}
  59  	as := x86.NewAssembler(pp)
  60  	as.Name = vt.String()
  61  	return as.Load(), nil
  62  }
  63  
  64  func pretouchTypeX86(_vt reflect.Type, opts option.CompileOptions, v uint8) (map[reflect.Type]uint8, error) {
  65  	/* compile function */
  66  	compiler := NewCompiler().apply(opts)
  67  
  68  	/* find or compile */
  69  	vt := rt.UnpackType(_vt)
  70  	if val := vars.GetProgram(vt); val != nil {
  71  		return nil, nil
  72  	} else if _, err := vars.ComputeProgram(vt, makeEncoderX86, v == 1); err == nil {
  73  		return compiler.rec, nil
  74  	} else {
  75  		return nil, err
  76  	}
  77  }
  78  
  79