Getting LLVM/Clang to be 16 bit aligned - objective-c

I am working on a legacy project that has a large amount of files dating back to pre-OS X days. It's data has been 16 bit aligned for > 15 years. I would like to move to a full LLVM compilation but I can't seem to get 2 byte alignment working. Are there any compiler level options available for this? (previously using -malign-mac68k)
I am aware of the #pragma pack(2) option here. However that would require me to modify upwards of 1000 source files to include this. That it's a worst-case option, but it seems like a hack. Besides, if this is possible then surely there is a default option to set the alignment?

According to clang's sources, it does support mac68k alignment rules. It seems that right now you can enable it via "#pragma options align=mac68k" only. If you're ok with small clang hacking, then you can implement the cmdline option as well and submit the patch to clang.

I would suggest looking at #pragma pack (see http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx and http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html). It's relatively easy to use, and should work with any compiler Apple ships.

i use the latest gcc/gortran on osx compiled by the guys at ibm, if you read the gcc manual there are at least 8 different alignment optimizations to consider not just the bulk malign

Because system-level headers assume default alignment (they don't have directives to override alignment in most cases), changing the alignment for everything will break OS calls. As such, you don't want to be doing this.
Just write a script to apply your #pragma pack(2) to the source files in question, avoiding #includes. It's relatively easy and unlikely to cause unfortunate side-effects.

Related

JSTileMap generating warnings in ios7 SpriteKit template

I am copying JSTileMap and LFCGzipUtility into the project and when I run the application it generates several warnings for both files.
However, when I run the provided sample from github, I don't see any of those warnings.
I would like to know why is there a difference, and in case, what am I doing wrong?
For reference please visit, https://github.com/slycrel/JSTileMap
Clarification, I would like to know if there is anything I should change in the project settings, rather than debug the code that runs just fine and without any warnings on the provided sample at the links above.
if you are running your app in the iOS simulator 64 bit mode then the problem is that the files of JSTileMap are not updated to 64 bit architecture
Apple documents says
Code that relies on the NSInteger and CGFloat types needs to be carefully examined.
Start by building the app for the 64-bit runtime, fixing any warnings that occur as well as searching your code for specific 64-bit issues. For example:
Make sure all function calls have a proper prototype.
Avoid truncating 64-bit values by accidentally assigning them to a 32-bit data type.
Ensure that calculations are performed correctly in the 64-bit version of your app.
Create data structures whose layouts are identical in the 32-bit and 64-bit versions of your app (such as when you write a data file to iCloud).
here is the Apple's transition guide to 64 bit:
https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/Introduction/Introduction.html
if that is not your case the open the file JSTileMap.m, inside press command + f type warning and press enter
and you will find all the warnings that JSTileMap want you to see just comment all warning and thats it
Here i show you all the warnings
#warning these appear to be incorrect for iso maps when used for tile objects! Unsure why the math is different between objects and regular tiles.
#warning This needs to be optimized into tilemap layers like our regular layers above for performance reasons.
#warning the positioning is off here, seems to be bottom-left instead of top-left.
#warning need to write setTileGidAt:
Good Luck!!

can i make an equivalent to /* and */ for comment blocks?

Its driving me crazy, I spend soo much time getting it wrong, and then fixing it wrong.
I'm thinking of using -= and =- as delineators, but it probably means a lot of hours in learning how to fool the compiler into a substitution. Is this a quixotic quest? can such be done? has it been done already, albeit with different keystrokes?
I work alone. I don't collaborate.
So I don't mind a non-standard work environment
If I need to in the future i could make a scheme whereby both could work.
Not without building your own custom version of the preprocessor. Comment syntax is an inherent part of the language and is not designed to be configurable.
(Incidentally, -= is already a token in Objective-C — it means "assign to LHS the result of subtracting RHS from LHS.")
It should be possible to extend clang, then modify your Xcode builds to include your clang extensions. I have no personal experience with writing complier extension for clang, but I did work on a tool that extended cl.exe. Warning: this would be a very deep dive into the internals of the build system.
Extending Clang
Good Luck

C++14 supporting editor/IDE

I'm looking for some powerful programming environment for C/C++. In fact the only think i need is powerful source navigating + creating tool. Doesn't matter if its free or commercial. I prefer some linux tool, but it doesn't have to be necessary linux app.
What i need is some kind of editor with following capabilities:
more open files + tabs/buffers switching
highlighting (+ bracket matching, folding, etc...)
save sessions
preview window(when the cursors stops on some symbol, i have also an preview window, that shows me the definition of that symbol)
searching for uses of the symbol through code
intelligent completion (must support c++ 14!)
What would be nice:
code beautifizier or something similar
utf-8 support
editor templates(for example automatic comment header for modules, functions...)
other editor scripting
a terminal/console accessible from program || compiling debugging capabilities(just for to be able to compile the whole project without switching extensively to command line)
program flow visualization
Please do anyone around the whole world knows anything, that knows all of that?
I've tried several editors/IDEs, but all of them misses something. I've decided to emphasise missing support for new c++14 - it's now probably the biggest problem:
VIM
Amazing scripting capabilities
4 and 6: i've tried to use vim+ctags+cscope and vim+gtags + omnicomplete. But its not that usable. This doesn't work properly. Vim doesn't know c++ and it does not know the symbol context. Though i've been able to achieve basically the desired behaviour after hours of learning vim scripting, i'm not able to use it in even a little more complicated manner.(Lets say i have global variable and local variable with same names and different types - vim ofc does not recognize if im seeing now global or local only variable. And i'm not even talking about namespaces, etc....). Actually VIM now support c++14 with the help of clang(libclang) and there is awesome plugin called YouCompleteMe, that can use libclang, and its auto-complete really workds with c++14 code! And it makes the vim the only editor supporting c++14.. This plugin is now more or less usable in more editors(vim, emacs, sublime). It adds autocomplete, jump to definition and error messages.
c++14 supported
Source Insight
Amazing highlighting capabilities(different font for highlighting is amazing(especially for function names in function defintion header for example), also there is possibility to assign keyboard shortcut to cancel font differences, and leave only color highlighting(when you are bothered with different code alignment(due to different fonts)
Problems with 1) (though a hacked library for this called TabsSiPlus exists)
no terminal/console
Software is not maintained anymore, its not open source(and even not free) :(((
c++14 support missing
SlickEdit
Amazing customizing possibilities, can emulate vim editing style(WOW!)
no terminal/console(actually there is one, but not that good, however it has builtin compiling capabilities
it has strange GUI - a lot of space is occupied by scrollbars, different window captions, etc..
its kinda expensive(comparing to others)
c++14 support missing
Kdevelop4
Very pleasant GUI, nice console, very fast, can emulate vi editing style(not too well though)
miss force rescan/retag project (sometimes there are wrong symbol references, especially in bigger projects - this bugs me a lot)
its preview window doesn't show definition, only reference to definition, the definition then is showed in main editing window.. - cannot see actual code and the symbol definition at once :((
c++14 support missing
Eclipse
nice environment with lots of functionality.
don't have ad4), however it has some sort of preview bubble. When you hover mouse cursor upon symbol, it will show a bubble with symbol definition. Its nice functionality, but unfortunately you need to use mouse, and its noway that user friendly as separate preview window.
also its a little slow, but i think its due to realtime syntax checking, and it probably will be disablable.
c++14 support missing
Emacs
Support c++14 by similar plugins as vim(YouCompleteMe)(see in Vim section).
Please can some proficient Emacs user fill in this section?
c++14 supported
Sublime Text 3
Not free
Support c++14 by similar plugins as vim(YouCompleteMe)(see in Vim section). Though i had some problems with it. Plugin was more unstable than in Vim for me.
Through different plugins it support more or less the same functionality(from user view) as vim.
c++14 supported
Do any of you use some different editor, that can do anything from the list above, or maybe some plugins/scripts that can achieve the demanded functionality?
Regarding VIm and your point 6, I have heard that clang_complete, which is leveraging clang from LLVM compiler was quite accurate but I have not yet tested myself.
Try Eclipse, can handle almost everything (from Java to C with GNU Tools)
Eclipse supports c++14, all you need to do is to add -std=c++1y in your prefrences->C/C++->Build->Settings->Discovery
for "CDT Cross GCC Built-in Compiler Setting" add "-std=c++1y" at the end of compiler specs.
You also set the dialects in your project setting to support c++14,
Go to your project setting->C/C++ Build->Settings->GCC C++ Compiler->Dialect and select -std=c++1y

Using open source SNES emulator code to turn a rom file into a self-contained executable game

Would it be possible to take the source code from a SNES emulator (or any other game system emulator for that matter) and a game ROM for the system, and somehow create a single self-contained executable that lets you play that particular ROM without needing either the individual rom or the emulator itself to play? Would it be difficult, assuming you've already got the rom and the emulator source code to work with?
It shouldn't be too difficult if you have the emulator source code. You can use a method that is often used to store images in c source files.
Basically, what you need to do is create a char * variable in a header file, and store the contents of the rom file in that variable. You may want to write a script to automate this for you.
Then, you will need to alter the source code so that instead of reading the rom in from a file, it uses the in memory version of the rom, stored in your variable and included from your header file.
It may require a little bit of work if you need to emulate file pointers and such, or you may be lucky and find that the rom loading function just loads the whole file in at once. In this case it would probably be as simple as replacing the file load function with a function to return your pointer.
However, be careful for licensing issues. If the emulator is licensed under the GPL, you may not be legally allowed to store a proprietary file in the executable, so it would be worth checking that, especially before you release / distribute it (if you plan to do so).
Yes, more than possible, been done many times. Google: static binary translation. Graham Toal has a good howto paper on the subject, should show up early in the hits. There may be some code out there I may have left some code out there.
Completely removing the rom may be a bit more work than you think, but not using an emulator, definitely possible. Actually, both requirements are possible and you may be surprised how many of the handheld console games or set top box games are translated and not emulated. Esp platforms like those from Nintendo where there isnt enough processing power to emulate in real time.
You need a good emulator as a reference and/or write your own emulator as a reference. Then you need to write a disassembler, then you have that disassembler generate C code (please dont try to translate directly to another target, I made that mistake once, C is portable and the compilers will take care of a lot of dead code elimination for you). So an instruction of a make believe instruction set might be:
add r0,r0,#2
And that may translate into:
//add r0,r0,#2
r0=r0+2;
do_zflag(r0);
do_nflag(r0);
It looks like the SNES is related to the 6502 which is what Asteroids used, which is the translation I have been working on off and on for a while now as a hobby. The emulator you are using is probably written and tuned for runtime performance and may be difficult at best to use as a reference and to check in lock step with the translated code. The 6502 is nice because compared to say the z80 there really are not that many instructions. As with any variable word length instruction set the disassembler is your first big hurdle. Do not think linearly, think execution order, think like an emulator, you cannot linearly translate instructions from zero to N or N down to zero. You have to follow all the possible execution paths, marking bytes in the rom as being the first byte of an instruction, and not the first byte of an instruction. Some bytes you can decode as data and if you choose mark those, otherwise assume all other bytes are data or fill. Figuring out what to do with this data to get rid of the rom is the problem with getting rid of the rom. Some code addresses data directly others use register indirect meaning at translation time you have no idea where that data is or how much of it there is. Once you have marked all the starting bytes for instructions then it is a trivial task to walk the rom from zero to N disassembling and or translating.
Good luck, enjoy, it is well worth the experience.

Any Macro or Technic for Part Optimization?

I am working on lock free structure with g++ compiler. It seems that with -o1 switch, g++ will change the execution order of my code. How can I forbid g++'s optimization on certain part of my code while maintain the optimization to other part? I know I can split it to two files and link them, but it looks ugly.
If you find that gcc changes the order of execution in your code, you should consider using a memory barrier. Just don't assume that volatile variables will protect you from that issue. They will only make sure that in a single thread, the behavior is what the language guarantees, and will always read variables from their memory location to account for changes "invisible" to the executing code. (e.g changes to a variable done by a signal handler).
GCC supports OpenMP since version 4.2. You can use it to create a memory barrier with a special #pragma directive.
A very good insight about locking free code is this PDF by Herb Sutter and Andrei Alexandrescu: C++ and the Perils of Double-Checked Locking
You can use a function attribute "__attribute__ ((optimize 0))" to set the optimization for a single function, or "#pragma GCC optimize" for a block of code. These are only for GCC 4.4, though, I think - check your GCC manual. If they aren't supported, separation of the source is your only option.
I would also say, though, that if your code fails with optimization turned on, it is most likely that your code is just wrong, especially as you're trying to do something that is fundamentally very difficult. The processor will potentially perform reordering on your code (within the limits of sequential consistency) so any re-ordering that you're getting with GCC could potentially occur anyway.