Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2b9d9142c5
Fetching contributors…

Cannot retrieve contributors at this time

file 321 lines (253 sloc) 7.23 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/*
* Copyright (C) 2005-2006 Pekka Enberg
*
* This file is released under the GPL version 2. Please refer to the file
* LICENSE for details.
*
* The file contains functions for converting Java bytecode method invocation
* and return instructions to immediate representation of the JIT compiler.
*/

#include "jit/bytecode-to-ir.h"

#include "jit/statement.h"
#include "jit/compiler.h"
#include "jit/args.h"

#include "vm/bytecode.h"
#include "vm/method.h"
#include "vm/class.h"
#include "vm/die.h"
#include "vm/jni.h"

#include "lib/stack.h"

#include <string.h>
#include <errno.h>
#include <stdio.h>

int convert_xreturn(struct parse_context *ctx)
{
struct expression *expr;
struct statement *return_stmt = alloc_statement(STMT_RETURN);
if (!return_stmt)
return warn("out of memory"), -ENOMEM;

expr = stack_pop(ctx->bb->mimic_stack);
return_stmt->return_value = &expr->node;
convert_statement(ctx, return_stmt);
clear_mimic_stack(ctx->bb->mimic_stack);
return 0;
}

int convert_return(struct parse_context *ctx)
{
struct statement *return_stmt = alloc_statement(STMT_VOID_RETURN);
if (!return_stmt)
return warn("out of memory"), -ENOMEM;

return_stmt->return_value = NULL;

convert_statement(ctx, return_stmt);
clear_mimic_stack(ctx->bb->mimic_stack);
return 0;
}

static unsigned int method_real_argument_count(struct vm_method *invoke_target)
{
int argc;

argc = vm_method_arg_slots(invoke_target);
if (!vm_method_is_static(invoke_target))
argc++;

return argc;
}

static int convert_and_add_args(struct parse_context *ctx,
struct vm_method *invoke_target,
struct statement *stmt)
{
struct expression **args_array;
struct expression *args_list;
unsigned long nr_args, i;

nr_args = method_real_argument_count(invoke_target);

args_array = pop_args(ctx->bb->mimic_stack, nr_args);
if (!args_array)
return warn("out of memory"), -EINVAL;

for (i = 0; i < nr_args; i++) {
struct expression *expr = args_array[i];

if (!expr_is_pure(expr))
args_array[i] = get_pure_expr(ctx, expr);

if (vm_type_is_pair(expr->vm_type))
i++;

if (i >= nr_args)
break;
}

args_list = convert_args(args_array, nr_args, invoke_target);
if (!args_list) {
free(args_array);
return warn("out of memory"), -EINVAL;
}

free(args_array);

stmt->args_list = &args_list->node;

return 0;
}

static struct statement * invoke_stmt(struct parse_context *ctx,
enum statement_type type,
struct vm_method *target)
{
struct statement *stmt;
enum vm_type return_type;

assert(type == STMT_INVOKE || type == STMT_INVOKEVIRTUAL ||
type == STMT_INVOKEINTERFACE);

stmt = alloc_statement(type);
if (!stmt)
return NULL;

stmt->target_method = target;

return_type = method_return_type(target);
switch (return_type) {
case J_VOID:
stmt->invoke_result = NULL;
break;
case J_LONG:
case J_FLOAT:
case J_DOUBLE:
stmt->invoke_result = temporary_expr(return_type, ctx->cu);
break;
case J_BYTE:
case J_BOOLEAN:
case J_CHAR:
case J_INT:
case J_SHORT:
stmt->invoke_result = temporary_expr(J_INT, ctx->cu);
break;
case J_REFERENCE:
stmt->invoke_result = temporary_expr(J_REFERENCE, ctx->cu);
break;
default:
error("invalid method return type %d", return_type);
};

return stmt;
}

static void insert_invoke_stmt(struct parse_context *ctx, struct statement *stmt)
{
convert_statement(ctx, stmt);

if (stmt->invoke_result) {
expr_get(stmt->invoke_result);
convert_expression(ctx, stmt->invoke_result);
}
}

static struct vm_method *resolve_invoke_target(struct parse_context *ctx, uint16_t access_flags)
{
unsigned long idx;

idx = bytecode_read_u16(ctx->buffer);

return vm_class_resolve_method_recursive(ctx->cu->method->class, idx, access_flags);
}

static struct vm_method *resolve_invokeinterface_target(struct parse_context *ctx)
{
unsigned long idx;

idx = bytecode_read_u16(ctx->buffer);

return vm_class_resolve_interface_method_recursive(ctx->cu->method->class, idx);
}

static void null_check_arg(struct expression *arg)
{
struct expression *expr;

expr = null_check_expr(to_expr(arg->arg_expression));
arg->arg_expression = &expr->node;
}

/**
* Searches the @arg list for EXPR_ARG_THIS and encapsulates its
* arg_expression in null check expression.
*/
static void null_check_this_arg(struct expression *arg)
{
if (expr_type(arg) != EXPR_ARGS_LIST) {
if (expr_type(arg) == EXPR_ARG_THIS)
null_check_arg(arg);

return;
}

struct expression *right_arg = to_expr(arg->args_right);
if (expr_type(right_arg) == EXPR_ARG_THIS) {
null_check_arg(right_arg);
return;
}

null_check_this_arg(to_expr(arg->args_left));
}

int convert_invokeinterface(struct parse_context *ctx)
{
struct vm_method *invoke_target;
struct statement *stmt;
int count;
int zero;
int err;

invoke_target = resolve_invokeinterface_target(ctx);
if (!invoke_target)
return warn("unable to resolve invocation target"), -EINVAL;

stmt = invoke_stmt(ctx, STMT_INVOKEINTERFACE, invoke_target);
if (!stmt)
return warn("out of memory"), -ENOMEM;

count = bytecode_read_u8(ctx->buffer);
if (count == 0)
return warn("invokeinterface count must not be zero"), -EINVAL;

zero = bytecode_read_u8(ctx->buffer);
if (zero != 0) {
return warn("invokeinterface fourth operand byte not zero"),
-EINVAL;
}

err = convert_and_add_args(ctx, invoke_target, stmt);
if (err)
goto failed;

insert_invoke_stmt(ctx, stmt);
return 0;

failed:
free_statement(stmt);
return err;
}

int convert_invokevirtual(struct parse_context *ctx)
{
struct vm_method *invoke_target;
struct statement *stmt;
int err = -ENOMEM;

invoke_target = resolve_invoke_target(ctx, 0);
if (!invoke_target)
return warn("unable to resolve invocation target"), -EINVAL;

stmt = invoke_stmt(ctx, STMT_INVOKEVIRTUAL, invoke_target);
if (!stmt)
return warn("out of memory"), -ENOMEM;

err = convert_and_add_args(ctx, invoke_target, stmt);
if (err)
goto failed;

insert_invoke_stmt(ctx, stmt);
return 0;
      failed:
free_statement(stmt);
return err;
}

int convert_invokespecial(struct parse_context *ctx)
{
struct vm_method *invoke_target;
struct statement *stmt;
int err;

invoke_target = resolve_invoke_target(ctx, CAFEBABE_CLASS_ACC_STATIC);
if (!invoke_target)
return warn("unable to resolve invocation target"), -EINVAL;

stmt = invoke_stmt(ctx, STMT_INVOKE, invoke_target);
if (!stmt)
return warn("out of memory"), -ENOMEM;

err = convert_and_add_args(ctx, invoke_target, stmt);
if (err)
goto failed;

null_check_this_arg(to_expr(stmt->args_list));

insert_invoke_stmt(ctx, stmt);
return 0;
      failed:
free_statement(stmt);
return err;
}

int convert_invokestatic(struct parse_context *ctx)
{
struct vm_method *invoke_target;
struct statement *stmt;
int err;

invoke_target = resolve_invoke_target(ctx, CAFEBABE_CLASS_ACC_STATIC);
if (!invoke_target)
return warn("unable to resolve invocation target"), -EINVAL;

stmt = invoke_stmt(ctx, STMT_INVOKE, invoke_target);
if (!stmt)
return warn("out of memory"), -ENOMEM;

err = convert_and_add_args(ctx, invoke_target, stmt);
if (err)
goto failed;

insert_invoke_stmt(ctx, stmt);
return 0;
      failed:
free_statement(stmt);
return err;
}
Something went wrong with that request. Please try again.