groovyc adds statements after ireturn in bytecode - groovy

Simple experiment where a method adds two ints.
First in Java:
gravadlax#durum byte_me> cat Add.java
public class Add {
public int add(int i, int j) {
return i + j;
}
}
Which leads to nothing unexpected in the bytecode (irrelevant parts removed):
gravadlax#durum byte_me> javac -g:none Add.java && javap -c Add
public int add(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: ireturn
Now in Groovy, using #CompileStatic:
gravadlax#durum byte_me> cat Add.groovy
#groovy.transform.CompileStatic
class Add {
int add(int i, int j) {
i + j
}
}
And the bytecode:
gravadlax#durum byte_me> groovyc Add.groovy && javap -c Add
public int add(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: ireturn
4: ldc #34 // int 0
6: ireturn
Now that's weird. What are 4 and 6 doing here? Not that it would change anything in practice, but out of curiosity, why would those two extra statements be generated?
Groovy Version: 2.3.4 JVM: 1.7.0_60 Vendor: Oracle Corporation OS: Mac OS X
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

Related

CRC32 in Java ME

Anyway of creating a 'CRC32' checksum value in 'Java ME (Micro Edition)'?
The Micro Edition SDK I am using does not include CRC methods or 'Biginteger', and anything I migrate from C is running into problems with signed long.
Found source code for CRC32 in 'Java'
http://developer.classpath.org/doc/java/util/zip/CRC32-source.html
public void update (int bval)
{
int c = ~crc;
c = crc_table[(c ^ bval) & 0xff] ^ (c >>> 8);
crc = ~c;
}

Implementing rint() in x86-64

MSVC 2012 doesn't have the rint() function. For 32-bit, I'm using the following:
double rint(double x) {
__asm {
fld x
frndint
}
}
This doesn't work in x64. There's _mm_round_sd() but that requires SSE4. What is an efficient preferrably branchless way of getting the same behavior?
rint 64-bit mode
#include <emmintrin.h>
static inline double rint (double const x) {
return (double)_mm_cvtsd_si32(_mm_load_sd(&x));
}
See Agner Fog's Optimizing C++ manual for lrint
32-bit mode
// Example 14.19
static inline int lrint (double const x) { // Round to nearest integer
int n;
#if defined(__unix__) || defined(__GNUC__)
// 32-bit Linux, Gnu/AT&T syntax:
__asm ("fldl %1 \n fistpl %0 " : "=m"(n) : "m"(x) : "memory" );
#else
// 32-bit Windows, Intel/MASM syntax:
__asm fld qword ptr x;
__asm fistp dword ptr n;
#endif
return n;
}
64-bit mode
// Example 14.21. // Only for SSE2 or x64
#include <emmintrin.h>
static inline int lrint (double const x) {
return _mm_cvtsd_si32(_mm_load_sd(&x));
}
Edit:
I just realized that this method will limit the values to to +/- 2^31. If you want a version with a larger range with SSE2 it's complicated (but easy with SSE4.1). See the round function in Agner Fog's Vector Class in the file vectorf128.h for an example.

Portable multithreading support in bytecodes/intermediate languages/compiler backends?

I've been working on the parser for a programming language that requires multithreading support. While investigating what the backend of my compiler should be, I noticed that I cannot find much information on multithreading for things like CIL, LLVM IR, gcc RTL, or JVM bytecode. I can find some references on how to make such code thread safe, but nothing on how to, say, create or fork threads. I can of course use signals or something to interface directly with the operating system, but that's nonportable and error-prone.
Is it the case that there's simply no portable way for managing threads in these low-level languages? Should I compile to a high(er)-level language like C instead?
In JVM byte code you can use any Java libraries, including those that work with threads. The conventional way of creating a thread would be
new Thread() {
#Override
public void run() {
/// code
}
}.start();
This code is written in Java. The corresponding JVM byte code can be seen by using javap:
0: new #2 // class Main$1
3: dup
4: invokespecial #3 // Method Main$1."<init>":()V
7: invokevirtual #4 // Method Main$1.start:()V
10: return
And Main$1 is a class:
final class Main$1 extends java/lang/Thread {
// compiled from: Intf.java
OUTERCLASS Main main ([Ljava/lang/String;)V
// access flags 0x8
static INNERCLASS Main$1 null null
// access flags 0x0
<init>()V
L0
LINENUMBER 7 L0
ALOAD 0
INVOKESPECIAL java/lang/Thread.<init> ()V
RETURN
L1
LOCALVARIABLE this LMain$1; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
// access flags 0x1
public run()V
L0
LINENUMBER 11 L0
RETURN
L1
LOCALVARIABLE this LMain$1; L0 L1 0
MAXSTACK = 0
MAXLOCALS = 1
}
This code is perfectly portable.

OpenCL: struct field initialization from inline function does not work

I have an OpenCL kernel code, which does not behave as expected. The similar C code compiled with gcc works fine.
struct data {
short* a;
};
typedef struct data Data;
inline void foo(Data* d) {
short b[1] = {99};
d->a = b;
}
__kernel void bar(__global short* output) {
Data d;
foo(&d);
short val = d.a[0];
int id = get_global_id(0);
output[id] = val;
}
Always outputs [0, 0, ..., 0].
If I initialize d.a in __kernel bar and only assign d->a[0] = 99 in foo it works as expected and outputs [99, 99, ..., 99]
Thanks in advance!
UPDATE:
I'm using Java and JOCL for the host code.
As ScottD suggested I've changed d->a = b; in function foo to *d->a = *b;.And it works great in C version. But causes the following error for OpenCL on MacOS:
Exception in thread "main" org.jocl.CLException:
CL_BUILD_PROGRAM_FAILURE Build log for device 0:
CVMS_ERROR_COMPILER_FAILURE: CVMS compiler has crashed or hung building an element.
at org.jocl.CL.clBuildProgram(CL.java:9368)
...
Or a JVM termination on Windows with AMD CPU:
# A fatal error has been detected by the Java Runtime Environment:
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007fedfeb007a, pid=3816, tid=4124
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0-b17 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [amdocl64.dll+0x60007a]
I believe the problem is this: Function foo sets a pointer used by the caller to the address of a local variable that goes out of scope when foo returns. When the caller accesses that pointer, data in the out of scope variable may or may not still be 99. To demonstrate, make a gcc debug build for this code. It works. Now add a printf(hello\n") after foo(&d) and before val=d.a[0]. Now it fails. This is because the printf call overites the stack memory containing the out of scope 99 value.
Probably you intended:
*d->a = *b; in place of d->a = b;

Why is this wrong!? Generating strings

I've been trying to generate strings in this way:
a
b
.
.
z
aa
ab
.
.
zz
.
.
.
.
zzzz
And I want to know why Segmentation fault (core dumped) error is prompted when it reaches 'yz'. I know my code don't cover all the posibles strings like 'zb' or 'zc', but that's not all the point, I want to know why this error. I am not a master in coding as you see so please try to explain it clearly. Thanks :)
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
void move_positions (char s[]);
int main (int argc, char *argv[])
{
char s[28];
s[0] = ' ';
s[1] = '\0';
int a = 0;
for(int r = 'a'; r <= 'z'; r++)
{
for(int t ='a';t <='z'; t++)
{
for(int u = 'a';u <= 'z'; u++)
{
for(int y = 'a'; y <= 'z'; y++)
{
s[a] = (char)y;
printf ("%s\n", s);
if (s[0] == 'z')
{
move_positions(s);
a++;
}
}
s[a-1] = (char)u;
}
s[a-2] = (char)t;
}
s[a-3] = (char)r;
}
return 0;
}
void move_positions (char s[])
{
char z[28];
z[0] = ' ';
z[1] = '\0';
strcpy(s, strcat(z, s));
}
First, let's compile with debugging turned on:
gcc -g prog.c -o prog
Now let's run it under a debugger:
> gdb prog
GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug 5 03:00:42 UTC 2012)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .. done
(gdb) run
Starting program: /Users/andrew/Documents/programming/sx/13422880/prog
Reading symbols for shared libraries +............................. done
a
b
c
d
e
...
yu
yv
yw
yx
yy
yz
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00007fffc0bff6c5
0x0000000100000c83 in main (argc=1, argv=0x7fff5fbff728) at prog.c:22
22 s[a] = (char)y;
Ok, it crashed on line 22, trying to do s[a] = (char)y. What's a?
(gdb) p a
$1 = 1627389953
So you're setting the ~1.6 millionth entry of the array s. What is s?
(gdb) ptype s
type = char [28]
Saving 1.6 million entries in a 28-element array? That's not going to work. Looks like you need to reset a to zero at the start of some of your loops.

Resources