Skip to content

Commit

Permalink
Work on heap and String management
Browse files Browse the repository at this point in the history
  • Loading branch information
chrlns committed Dec 26, 2023
1 parent da462f8 commit d57a9ff
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 6 deletions.
Binary file not shown.
Binary file added doc/jvms21.pdf
Binary file not shown.
10 changes: 8 additions & 2 deletions src/exec/ldc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ void do_LDC(Thread *thread) {
break;
}
case CONSTANTPOOL_STRING: {
// A reference to a String instance should be put onto the stack
value->data.ptr = item->Data; /* Pointer to a CONSTANT_STRING_INFO structure */
/* A reference to a String instance should be put onto the stack */
uint16_t stridx = ((struct CONSTANT_STRING_INFO*)item->Data)->StringIndex;
uint16_t strlen = ((struct CONSTANT_UTF8_INFO*)&frame->constants[stridx - 1])->Length;
char* strtext = ((struct CONSTANT_UTF8_INFO*)&frame->constants[stridx - 1])->Text;



value->data.ptr = NULL;
dbgmsg("Not implemented");
exit(-10);
break;
Expand Down
14 changes: 14 additions & 0 deletions src/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@
*/

#include <vm.h>

extern VM* vm;

/**
* @brief Creates a new Object on the heap, registers it with the GC and
* returns a pointer to it.
*
* @param clazz
* @return Object*
*/
Object* Heap_newObject(const Class* clazz)
{
Object* str = (Object*)malloc(sizeof(Object));
}
26 changes: 26 additions & 0 deletions src/include/heap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Bean Java VM
* Copyright (C) 2005-2023 Christian Lins <christian@lins.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _HEAP_H_
#define _HEAP_H_

#include <class.h>
#include <object.h>

Object* Heap_newObject(const Class* clazz);

#endif
25 changes: 25 additions & 0 deletions src/include/javastring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Bean Java VM
* Copyright (C) 2005-2023 Christian Lins <christian@lins.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _JAVASTRING_H_
#define _JAVASTRING_H_

#include <object.h>

Object* String_new(const uint16_t len, const char* text);

#endif
6 changes: 6 additions & 0 deletions src/include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ typedef struct Object {

Varframe* fields;
uint16_t fields_num;

/* Fields necessary for heap and gc management */
bool gc_marked;
Object* next;
} Object;

Object* Object_new(const Class* clazz);

#endif
7 changes: 4 additions & 3 deletions src/include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@

#include <thread.h>
#include <class.h>
#include <classloader.h>
#include <debug.h>
#include <object.h>
#include <stackframe.h>
#include <classloader.h>
#include <linker.h>

#define isAccessFlag(method, accFlag) \
Expand Down Expand Up @@ -73,8 +74,8 @@ typedef struct VM {
unsigned char ThreadNum;
struct MONITOR* Monitors; /* Monitors */

unsigned int* MemoryHeap; /* Array of int (pointern) representing the
GC-Heap of the application. */
Object* heapHead; /* Head of the linked list representing the heap */

bool DoCollectGarbage; /* This is true if the Garbage Collector must
be called. */
} VM;
Expand Down
29 changes: 29 additions & 0 deletions src/javastring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Bean Java VM
* Copyright (C) 2005-2023 Christian Lins <christian@lins.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <assert.h>

#include <javastring.h>

Object* String_new(const uint16_t len, const char* text)
{
/* Ensure the String class is loaded */
Class* clazz = Classloader_forName("java/lang/String");
assert(clazz != NULL);


}
25 changes: 25 additions & 0 deletions src/object.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Bean Java VM
* Copyright (C) 2005-2015 Christian Lins <christian@lins.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <class.h>
#include <heap.h>
#include <object.h>

Object* Object_new(const Class* clazz)
{
return Heap_newObject(clazz);
}
2 changes: 1 addition & 1 deletion src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int start_process(Class* new_class)
if (clinitMethod == NULL) {
dbgmsg("No class constructor found. Continue with main.");
} else {
// Invoke class constructor using special INVOKE instruction
/* Invoke class constructor using special INVOKE instruction */
dbgmsg("Invoke <clinit>");
Stackframe_create_init_push(&(vm->Threads[0]), new_class, clinitMethod);
}
Expand Down

0 comments on commit d57a9ff

Please sign in to comment.