r/AskComputerScience • • Nov 22 '20

Why do we need Rosetta when changing from Intel to M1 chips if applications nowadays are written in high level languages?

From my understanding, high level languages are compiled into machine code readable by the chip. Why do we need to translate things on the fly with Rosetta if we can just take the source code of the application and just recompile it for the new chip? Let me know if I’m missing something here.

31 Upvotes

27 comments sorted by

18

u/jhaluska Nov 22 '20

You're not missing anything.

  1. Recompiling to a new architecture usually works, but they have to be written in architecture independent ways. Developers often create optimizations for a single platform and trade off portability. This is also a big issue when changing endianess in architectures.

  2. It requires all the developers to recompile. Some of those companies may have gone under or the developers were hit by a bus. Trying to get everybody to do that at once is impossible.

So expect to use Rosetta and then see programs get recompiled over time. Apple ran into this same problem before when switching to Intel processors.

5

u/uddeeeeerrrrs Nov 22 '20

ohhhh i see. why can’t apple just decompile the machine code and then recompile it for their new chip? Decompilers does not seem impossible to implement given I can easily find one for Java on the web.

19

u/jhaluska Nov 22 '20

why can’t apple just decompile the machine code and then recompile it for their new chip?

That's basically what Rosetta is doing. The problem is it can lose significant performance compared to compiling natively for the platform as it might have to emulate instructions that the new architecture doesn't support.

It's like taking a book A and translating it into language B. If you translate B into a third language C, it won't be as good as translating A into C.

5

u/uddeeeeerrrrs Nov 22 '20

I see. That makes a lot of sense. Thanks!

2

u/juanfnavarror Nov 23 '20

Although the book language analogy is genius, the part where he says that one is worse than the other is true but not for the same reason. In the book analogy the translation gets worse due to loss of meaning as translation errors stack up. In the CPU example, the translation gets worse by loss of efficiency, doing more work for the same tasks by emulating code optimized for another architecture (what jhaluska mentioned).

2

u/evolseven Nov 22 '20

Java and .Net are 2 languages that don't compile to pure assembly. They compile to something called bytecode (or something similar), and the runtime actually converts that bytecode to assembly on the fly. Decompiling Java is much simpler and more accurate than decompiling C, and depending on how it was compiled you can get original variable names\code. Decompilers do exist but they are not nearly as clean as java when decompiled.

0

u/FoxBearBear Nov 22 '20

Same bus tho?

5

u/armpit_puppet Nov 22 '20

I think what you’re missing here is that macOS apps are compiled versions of the source code. When you download an app, the source code is not available to you.

Rosetta fixes this by translating on the fly, as you mentioned.

However, Apple has another trick up their sleeve. Rosetta apps will actually be 2 apps in one, an Intel version and an Apple silicon version.

During this transition period, application developers compile the source code for both Intel and Apple chips. Apps will have both versions, and Rosetta will pick the best one for the machine.

So it does work similar to how you said, but initiated by the application developer.

You can read more in Apple’s Rosetta documentation

2

u/uddeeeeerrrrs Nov 22 '20

I see. Why cant we decompile the compiled versions and recompile them? it seems easy to find say a Java decompiler online

4

u/wolverineoflove Nov 22 '20

Java usually runs in a virtual machine and runs a form of byte code (intermediate representation). Hence Java can reinterpret the "compiled version" a bit easier than gcc/clang binaries. For Objective-C and Swift, when highly optimized and compiled your suggestion is not usually practical. It would also require development tooling on any random persons Mac. In the end emulation is the correct solution.

2

u/josh2751 Nov 22 '20

You cannot realistically "decompile" C, C++, Objective-C, or Swift and then recompile them into a working application.

1

u/armpit_puppet Nov 22 '20

That’s what Rosetta does:

To the user, Rosetta is mostly transparent. If an executable contains only Intel instructions, macOS automatically launches Rosetta and begins the translation process. When translation finishes, the system launches the translated executable in place of the original. However, the translation process takes time, so users might perceive that translated apps launch or run more slowly at times

Why do you want to decompile then recompile?

1

u/Shabam999 Nov 22 '20

Decompilers aren't that great and don't even always work. High level code to machine code isn't a 1-1 and you lose a ton of optimization in the process.

1 major reason is that compilers take your high level code and make a ton of optimizations based specifically on the architecture. Even if you completely turn off optimization (say using -O0 (letter oh + number zero) in g++) there'll still a lot of stuff that's very architecture specific that a decompiler will struggle with to translate into high level code.

What this results in is decompilers end up creating very messy and obfuscated code so when it eventually gets recompiled, the next compiler can't really optimize anything and ends up creating very inefficient machine code which results in slow programs.

A lot of times, it ends up being more efficient to just open up a VM and run the old, optimized code in there. Even with the overhead of a VM, it can end up being more efficient overall.

1

u/drew8311 Nov 22 '20

However, Apple has another trick up their sleeve. Rosetta apps will actually be 2 apps in one, an Intel version and an Apple silicon version.

It actually has both stored physically on the computer? Wouldn't it make sense to be a download optimization?

1

u/armpit_puppet Nov 22 '20

Potentially for apps in the App Store (iOS App Store already has some of these optimizations). I don’t think there is a practical way to do the optimization for “regular” apps downloaded from a website.

1

u/drew8311 Nov 22 '20

Most download links have several choices, for windows I constantly see x86 or x64 (not a big deal since both usually work). In most cases it can detect the OS but the browser can do the same for CPU so its trivial to default to a recommended download link + have other options below it one of which can be the generic Mac one for people who are not sure.

1

u/armpit_puppet Nov 22 '20

The relationship between x86 and x64 are not the same as the relationship between x64 and ARM. An x64 processor is expected to be able to run x86, but x86 cannot run x64. ARM is a completely different architecture from x86/x64, and there is no way to run native x86/x64 instructions on ARM without translation.

It also seems like you’re proposing solutions where Apple has already dictated the process. Their solution is that 1 app contains 2 executables. It’s the same solution from the PowerPC to x86 transition.

I’m not familiar enough with Windows executables to know why there are separate links. But it seems like Apple’s design philosophy led them to think a larger app was more acceptable than multiple download links.

Also I’m very curious to know why it is trivial to have a default download link? Who is it trivial for? Browser makers? Website owners? App developers?

1

u/drew8311 Nov 22 '20

I know the relationship is not the same, but the download methods can be. Go to download on website for Mac, there is two links one for x64 the other for M1/ARM. Then if the website owner wanted to, they could detect which of those is the most likely candidate and have it on a default download button. Its the exact same concept most every site does already, check google.com/chrome as an example it knows your OS so there is already a single link and its the correct one.

1

u/LinkifyBot Nov 22 '20

I found links in your comment that were not hyperlinked:

I did the honors for you.


delete | information | <3

1

u/armpit_puppet Nov 23 '20

Yes, but why? It’s more confusing for everyone.

Users have a chance of downloading your app and it not working. Web developers have to add a preferred download feature. IT administrators have to worry about which bundle goes to which machine, which means more help-desk tickets.

A single bundle is easier for all humans, but takes more data.

Apple are selling machines at a premium price, to consumers in markets where bandwidth is not going to be a factor, and their brand ethos is about simplicity.

Yes, you can do it either way, but the economics don’t make sense in the Apple ecosystem.

1

u/juanfnavarror Nov 23 '20

Honestly nowadays storage space is pretty expendable and the average user won't care if the filesize is double, and maybe won't even know what the terms 'x86' or 'arm' mean. If the program is pretty field specific, you'd expect a professional user to have a higher storage disk anyways. Admittedly it's a pretty inefficient choice, and easy to solve as you mention.

43

u/[deleted] Nov 22 '20

Yes you are right. We can compile high level code into native ARM instructions. But developers will not compile their applications and make it available overnight for Apple's new computer. Moreover some programming languages do not yet have decent ARM compiler.

There are hundreds of thousands of x86 programs that needs to be available on Apple's silicon. Otherwise users will not be intersted in buying their shiny new computer. So Rosetta emulates x86 to make all those programs available to run on M1 on day 1 rather than wait for years for developers to publish native apps. So users can happily run their existing programs unmodified and Apple can make billions from their new computer.

3

u/wescotte Nov 22 '20 edited Nov 22 '20

It's a problem with ambiguity.

Programmers write code for one type of hardware but when compiled to run on another often doesn't quite do the same thing and it's not completely obvious when/where/how this happens.

Say you are blind and you develop a map of your home. You create a huge data set that instructions to get from one place to another based on the number of steps and rotations. It seems obvious that it only works for the exact layout of your home and any slight change in layout could break the entire map. However, even if you give the map to somebody for use in your home it probably still won't work. The size of their step and the amount of rotating they do per unit will not be the same as you.

This is basically what happens with programs as there are so many things baked into your instructions specific for the hardware you are running on. Now, you might think it would be simple to just include a key that says 1 step = X feet and a 1 rotation = Y degrees.

For our map use case that seems like the only missing piece of information to generalize it's purpose. However, in reality it's not that simple. What if the person using your map can only count to 100 but some paths have over 100 steps? You would have to adjust your map to limit paths to 100 steps max and/or create intermediate locations. That assumes the program was written in a way it can detect these types of memory limitations. It quickly becomes overwhelming to where you're spending more time generalizing your program to work on different hardware than actually writing the program.

So while you can always just recompile on different hardware depending on what you program does and how it does it can easily have unintended side effects. The more general you make your program the more complex it becomes write/maintain and typically the slower your program will operate.

2

u/Bottled_Void Nov 22 '20

"if we can just take the source code of the application and just recompile it"

Because Apple doesn't own the source code to every application.

1

u/Felicia_Svilling Nov 22 '20

You might want to run a program, even though you don't have access to its source code.

1

u/josh2751 Nov 22 '20

The binary format required on an X86/X64 processor is completely different from the binary format required for an ARM executable.

In most cases, you don't have the source code of the application to recompile it. The software manufacturer does, and if they wrote it in a portable way (this is a bad assumption for a bunch of reasons btw), then they can just recompile in Xcode and create a fat binary that can run under either X64 or Arm. If they didn't, they're going to have to fix whatever optimizations they made that are X64 dependent to make their code ARM compatible.

What Rosetta does is translate, ahead of time, the X64 machine code in an X64 binary, into ARM machine code, so that the translated binary can run on the M1 processor.

1

u/juanfnavarror Nov 23 '20 edited Nov 23 '20

Although not mainly relevant, another pro of such an emulation layer, is that some types of programs, such as videogames (which there are TENS OF THOUSANDS of) which are not being developed on anymore will not ever be ported/recompiled to ARM, so by using Rosetta you can cross out the main con of switching the architecture, which is the incompatibility of decades of now-legacy programs.