Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to string #233

Closed
dequeb opened this issue Mar 7, 2024 · 1 comment
Closed

Convert to string #233

dequeb opened this issue Mar 7, 2024 · 1 comment

Comments

@dequeb
Copy link

dequeb commented Mar 7, 2024

Hi,

I'm new to llir/llvm. I need a fonction to do type conversion. My first iteration would be to convert from int and float to string. I guess I'll have to call sprintf from the C library. I don't want to allocate global variables to do so. I saw that entry.malloc to allocate temporary variables. That's pretty much where I am. May I ask if someone could give me some cues on how to proceed?
I tried this, but I return an *ir.InstGetElementPtr:

func convertToString(ctx *Context, v value.Value) *ir.InstGetElementPtr {
	// convenient constants
	i32 := types.I32
	i8 := types.I8
	i8x50 := types.NewArray(50, i8)
	zero := constant.NewInt(i32, 0)

	//  buffer
	tmp1 := ctx.Block.NewAlloca(i32)
	ctx.Block.NewStore(zero, tmp1)
	tmp3 := compileStringConstant(ctx, "%d\x00", "")
	// convert float to string
	tmp4 := ctx.Block.NewGetElementPtr(i8x50, zero, zero)
	sprintf := ctx.lookupFunction("sprintf")
	ctx.Block.NewCall(sprintf, tmp4, tmp3, v)
	return tmp4
}

I still don't know how to use this value to send it to another function like print.

Here is my calling function:

// compilePrintStmt compiles a print statement.
func compilePrintStmt(ctx *Context, node *ast.SpecialStmt) {
	zero := constant.NewInt(types.I64, 0)
	printf := ctx.lookupFunction("printf")
	puts := ctx.lookupFunction("puts")
	var value *ir.Global
	var str *ir.InstGetElementPtr
	for _, arg := range node.Args {
		switch arg := arg.(type) {
		case *ast.BasicLit:
			switch arg.Kind {
			case token.StringLit:
				value = compileConstant(ctx, arg, "")
			case token.LongLit:
				str = convertToString(ctx, value)
			default:
				panic("unknown basic literal")
			}
		case *ast.Identifier:
			// TODO: generalize for any type of identifier, not only global constant
			value = ctx.lookupConstant(arg.Name)
		}
		typ := value.Typ.ElemType
		gep := constant.NewGetElementPtr(typ, value, zero, zero)
		// TODO: type conversion to string
		ctx.Block.NewCall(printf, gep)
	}
	if node.Semicolon == nil {
		value = ctx.lookupConstant("vbEmpty")
		typ := value.Typ.ElemType
		gep := constant.NewGetElementPtr(typ, value, zero, zero)
		ctx.Block.NewCall(puts, gep)
	}

@dequeb dequeb closed this as completed Mar 7, 2024
@dequeb
Copy link
Author

dequeb commented Mar 7, 2024

I will proceed differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant