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

InkBin Header #8

Merged
merged 8 commits into from
Feb 7, 2021
Merged

InkBin Header #8

merged 8 commits into from
Feb 7, 2021

Conversation

JBenda
Copy link
Owner

@JBenda JBenda commented Feb 1, 2021

Adding inkcpp version and endianness to InkBin header.

No STL support will come after the PoC finished.

@brwarner
Copy link
Collaborator

brwarner commented Feb 1, 2021

Looks good so far. I guess maybe Endianness should be read first though? Since that would affect the encoding of the version numbers (for when, someday in the future, we actually do the flip).

The memory layout of std::tuple is not intuitive and not defined from
the standard.
There for we will use an explicit read.

Check out this example: the output for me was:
`42 : 10`
where I expect:
`10 : 42`

compile flags: `--std=c++20`
compiler: `g++ 10.2.0`
```cpp

struct Foo {
	static Foo create(const char * data) {
		Foo foo;
		foo._data = *reinterpret_cast<const decltype(_data)*>(data);
		return foo;
	}
	int& x = std::get<0>(_data);
	int& y = std::get<1>(_data);
	std::tuple<int,int> _data;
};

int main(int argc, char *argv[])
{
	int buffer[] = {10, 42};
	auto f = Foo::create((char*)buffer);
	std::cout << f.x << " : " << f.y << std::endl;
	return 0;
}
```
@brwarner
Copy link
Collaborator

brwarner commented Feb 2, 2021

It's probably better to avoid std::tuple all together for consistency with the rest of the code base. The only STL code I have right now (which is all excluded if you turn off that preprocessor flag) is purely in user-facing code. My intent was that if you were already using STL and you wanted a prettier, STL friendly interface, you could turn on all those functions.

Admittedly, my reason for making STL optional in the runtime is not very compelling. Basically, since the project is supposed to work with Unreal, I didn't want to have all these and related STL includes when Unreal's API goes out of its way to implement their own versions of all those classes.

@JBenda
Copy link
Owner Author

JBenda commented Feb 2, 2021

I see, sorry will chage it.
It was a stupid idea anyway.

I would like to introduce a readNumber function to hide the endian handling.

Or would it be ok to rewrite the in memeory copy of the inkbin if the endian differs?

also we need a kind of logger to warn about it (so he can recompile it to avoid the overhead)

Is the inkcpp version number already in a header?

@brwarner
Copy link
Collaborator

brwarner commented Feb 2, 2021

I see, sorry will change it.
It was a stupid idea anyway.

Eh. Sometimes I try random C++ features just for fun to see how they work. I've never really used tuple much before actually.

I would like to introduce a readNumber function to hide the endian handling.
Or would it be ok to rewrite the in memeory copy of the inkbin if the endian differs?

In runner_impl.cpp there's a template function called read which handlers reading a data type from the binary block. I think what you'd want to do is put the endian handling in there. It's a member function of runner_impl so it has access to the story object which would contain the new header information. I think you'd just check if the current machine endianness differs from the file's and if so, flip the bytes. It reads types of varying sizes so this way we can handle all of them in one place.

also we need a kind of logger to warn about it (so he can recompile it to avoid the overhead)

I don't have a good runtime logger yet! That's something that should be added at some point.

Is the inkcpp version number already in a header?

No. There is really no inkcpp version at all right now. Maybe we can make a new header file and define it? The header is written in binary_emitter.cpp's output function. You'll see right now I just write out the Ink version that I got from the JSON.

@JBenda
Copy link
Owner Author

JBenda commented Feb 2, 2021

Thanks for the hints. The read function was a nice spot. It's also without STL 😜.
Is there more to-do for this feature? At the moment I set up a big endian system to check this :)

@brwarner
Copy link
Collaborator

brwarner commented Feb 3, 2021

Thanks for the hints

Hey, no problem.

Is there more to-do for this feature?

It looks like right now you're just always writing out "SAME" into the file for the endian encoding. I'm not sure if it makes sense to write SAME or DIFFER into the file, since the compiler doesn't really know who is going to be reading it. I think it made more sense to have what you had before: Big or Small being the enum values. Then the runtime can just check if its endian setting matches the file and if not, swap.

I did some googling concerning checking the endianness. Looks like this function works:

// source https://stackoverflow.com/questions/1001307/detecting-endianness-programmatically-in-a-c-program
bool is_big_endian(void)
{
    union {
        uint32_t i;
        char c[4];
    } bint = {0x01020304};

    return bint.c[0] == 1; 
}

I think you also want the value of the endianness enum to be only a single byte, otherwise the byte order could be wrong if you're reading from a different endianness.

@JBenda
Copy link
Owner Author

JBenda commented Feb 3, 2021

This was the idea, the compiler write SAME, and when the endian differs the runner reads DIFFER, so i do not care about the actual ending.

So we don't realy care about the actual endian. But we can also use explicit big and small endian.
I think it's just a matter of taste.

@brwarner
Copy link
Collaborator

brwarner commented Feb 3, 2021 via email

@JBenda JBenda changed the base branch from master to platform February 4, 2021 14:46
@JBenda
Copy link
Owner Author

JBenda commented Feb 4, 2021

Ok, have you somewhere a written form of you're coding style ^^, than I can adapt, and you can spare time ^^;

@JBenda JBenda marked this pull request as ready for review February 4, 2021 14:47
@JBenda JBenda changed the title InkBin Header [WIP] InkBin Header Feb 4, 2021
@JBenda
Copy link
Owner Author

JBenda commented Feb 4, 2021

What should be the reaction when the inkcpp version dose not match ? An warning or an exception?
It is still possible that a different Version will execute the programm correctly 🤔

@brwarner
Copy link
Collaborator

brwarner commented Feb 4, 2021

What should be the reaction when the inkcpp version dose not match ? An warning or an exception?
It is still possible that a different Version will execute the programm correctly 🤔

I think the reaction should be a runtime exception on load. My idea for the inkcpp version number is it should only increment when there are breaking changes to the format.

Ok, have you somewhere a written form of you're coding style ^^, than I can adapt, and you can spare time ^^;

I'll add a Contributing guide this weekend :)

@JBenda
Copy link
Owner Author

JBenda commented Feb 4, 2021

Cool :) 👍

Then I will add an exception on load.

Copy link
Collaborator

@brwarner brwarner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the code style guidelines as I mentioned. Mostly it's just casing. I added comments on everything that should change. Hopefully it's not too pedantic lol. I just like a unified naming scheme, whatever scheme it happens to be.

I liked to the corresponding section in each comment, but the full guide is here.

https://github.com/brwarner/inkcpp/blob/master/CONTRIBUTING.md


#include "system.h"

namespace ink {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ink::internal since this is not a user facing type.

https://github.com/brwarner/inkcpp/blob/master/CONTRIBUTING.md#namespaces

#include "system.h"

namespace ink {
struct Header {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DIFFER = 0x0100
} endien = ENDENSE::NONE;
uint32_t inkVersionNumber = 0;
uint32_t inkCppVersionNumber = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum type, values, and struct members should be in snake_case.

https://github.com/brwarner/inkcpp/blob/master/CONTRIBUTING.md#code-style

#include "system.h"

namespace ink {
constexpr uint32_t VERSION = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compile-time constants should be in PascalCase.

Also maybe name this something a bit more specific like InkBinVersion perhaps?

https://github.com/brwarner/inkcpp/blob/master/CONTRIBUTING.md#code-style


#ifdef INK_ENABLE_STL
#include <iostream>
#endif

namespace ink {
// FIXME: find appropriate place to define
Header Header::parse_header(const char *data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in a new header.cpp file located in the inkcpp folder. You can add new cpp files to CMake by adding them to the CMakeLists.txt file in that folder. You'll see a giant list of source files there. Since it's only used by the runtime, the code can live in that folder.


#ifdef INK_ENABLE_STL
#include <iostream>
#endif

namespace ink {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a struct internal, it should live in ink::internal

@@ -2,6 +2,7 @@

#include "command.h"
#include "system.h"
#include "version.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you don't actually use this header here? You can just include it in the relevant cpp file.

@brwarner
Copy link
Collaborator

brwarner commented Feb 5, 2021

Also, if you merge in the master branch you'll get the new GitHub Actions which will automatically test if your branch builds on each of the OSs.

@JBenda
Copy link
Owner Author

JBenda commented Feb 7, 2021

This sounds awesome :+1 !

But the platform is still not at the master, so I can't merge it there, because my branches based on the platform. Otherwise, I can't run them locale ^^.

@JBenda JBenda requested a review from brwarner February 7, 2021 14:00
Copy link
Collaborator

@brwarner brwarner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@brwarner brwarner merged commit b1d6085 into JBenda:platform Feb 7, 2021
brwarner added a commit that referenced this pull request Feb 7, 2021
@JBenda JBenda deleted the feature/InkBinHeader branch February 7, 2021 23:24
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

Successfully merging this pull request may close these issues.

2 participants